【POJ3294】 Life Forms(SA)
...又是TLE,对于单组数据肯定TLE不了,问题是多组的时候就呵呵了...
按height分组去搞,然后判一下是否不属于同一个串...
const maxn=;
var
x,y,rank,sa,c,col,h,rec:array[..maxn] of longint;
pd:array[..maxn] of boolean;
s:ansistring;
n,k,maxlen,cnta:longint; function max(x,y:longint):longint; begin if x>y then exit(x) else exit(y); end;
function min(x,y:longint):longint; begin if x<y then exit(x) else exit(y); end;
procedure swap(var x,y:longint);var tmp:longint; begin tmp:=x;x:=y;y:=tmp; end; procedure make;
var i,p,tot:longint;
begin
p:=;
while p<n do
begin
fillchar(c,sizeof(c),);
for i:= to n-p do y[i]:=rank[i+p];
for i:= n-p+ to n do y[i]:=;
for i:= to n do inc(c[y[i]]);
for i:= to n do inc(c[i],c[i-]);
for i:= to n do
begin
sa[c[y[i]]]:=i;
dec(c[y[i]]);
end;
fillchar(c,sizeof(c),);
for i:= to n do x[i]:=rank[i];
for i:= to n do inc(c[x[i]]);
for i:= to n do inc(c[i],c[i-]);
for i:= n downto do
begin
y[sa[i]]:=c[x[sa[i]]];
dec(c[x[sa[i]]]);
end;
for i:= to n do sa[y[i]]:=i;
tot:=;
rank[sa[]]:=;
for i:= to n do
begin
if (x[sa[i]]<>x[sa[i-]]) or (x[sa[i]+p]<>x[sa[i-]+p]) then inc(tot);
rank[sa[i]]:=tot;
end;
p:=p<<;
end;
for i:= to n do sa[rank[i]]:=i;
end; procedure makeh;
var i,p,j:longint;
begin
p:=; h[]:=;
for i:= to n do
begin
p:=max(p-,);
if rank[i]= then continue;
j:=sa[rank[i]-];
while (j+p<=n) and (i+p<=n) and (s[i+p]=s[j+p]) do inc(p);
h[rank[i]]:=p;
end;
end; procedure init;
var i,j,tot:longint;
s1:ansistring;
begin
k:=n>>;
readln(s);
tot:=;
maxlen:=length(s);
for i:= to maxlen do col[i]:=;
for i:= to n do
begin
readln(s1);
s:=s+'#';
maxlen:=max(length(s1),maxlen);
inc(tot);
for j:= length(s)+ to length(s)+length(s1) do col[j]:=tot;
s:=s+s1;
end;
n:=length(s);
fillchar(c,sizeof(c),);
for i:= to n do x[i]:=ord(s[i]);
for i:= to n do inc(c[x[i]]);
for i:= to do inc(c[i],c[i-]);
for i:= to n do
begin
sa[c[x[i]]]:=i;
dec(c[x[i]]);
end;
tot:=;
rank[sa[]]:=;
for i:= to n do
begin
if x[sa[i]]<>x[sa[i-]] then inc(tot);
rank[sa[i]]:=tot;
end;
make; makeh;
end; function check(x:longint):boolean;
var i,j,tot,cnt:longint;
begin
fillchar(pd,sizeof(pd),false);
pd[]:=true; tot:=; cnt:=;
if not pd[sa[]] then inc(tot);
pd[sa[]]:=true;
for i:= to n do
begin
if h[i]<x then
begin
if tot>k then
begin
inc(cnt);
rec[cnt]:=i-;
end;
tot:=;
fillchar(pd,sizeof(pd),false);
pd[]:=true;
end;
if not pd[col[sa[i]]] then inc(tot);
pd[col[sa[i]]]:=true;
end;
if tot>k then
begin
inc(cnt);
rec[cnt]:=i-;
end;
if cnt> then
begin
cnta:=cnt;
exit(true);
end;
exit(false);
end; procedure qsort(l,r:longint);
var i,j,mid,tmp:longint;
begin
if l>r then exit;
i:=l; j:=r;
mid:=rec[l+random(r-l+)];
repeat
while rec[i]<mid do inc(i);
while rec[j]>mid do dec(j);
if i<=j then
begin
swap(i,j);
inc(i); dec(j);
end;
until i>j;
qsort(i,r); qsort(l,j);
end; procedure solve;
var l,r,mid,tot,ans,i,j:longint;
begin
l:=; r:=maxlen; ans:=;
while l<=r do
begin
mid:=(l+r)>>;
if check(mid) then
begin
ans:=mid;
l:=mid+;
end
else r:=mid-;
end;
fillchar(pd,sizeof(pd),false);
if ans= then
begin
writeln('?');
exit;
end;
qsort(,cnta);
for i:= to cnta do writeln(copy(s,sa[rec[i]],ans));
end; Begin
readln(n);
while n<> do
begin
init;
solve;
writeln;
readln(n);
end;
End.
【POJ3294】 Life Forms(SA)的更多相关文章
- 【POJ3294】 Life Forms (后缀数组+二分)
Life Forms Description You may have wondered why most extraterrestrial life forms resemble humans, d ...
- 【poj3294】 Life Forms
http://poj.org/problem?id=3294 (题目链接) 题意 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. Solution 后缀数组论文题.. 将 n 个字符串 ...
- 【POJ3294】Life Forms(后缀数组,二分)
题意: n<=100 len[i]<=1000 思路:这是一道论文题 ..]of longint; ch:..]of ansistring; n,n1,l,r,mid,last,i,j,m ...
- 【转】Xamarin Forms 介绍
特此声明,本篇博文转自:http://blog.csdn.net/kinfey/article/details/29621381 什么是 Xamarin Forms ? Xamarin Forms 是 ...
- 【转】SQL2008的sa账户被禁用,其他账户无法连接的解决方法
或者你还有其它的sysadmin权限的账号,你可以用此账号登录,重置SA密码. 但是在以下情况下,怎么办呢? 1. SA密码丢失或者SA账号被禁用. 2. 你进行了一些安全操作,把BuiltinAdm ...
- 【POJ3415】 Common Substrings (SA+单调栈)
这道是求长度不小于 k 的公共子串的个数...很不幸,我又TLE了... 解法参考论文以及下面的链接 http://www.cnblogs.com/vongang/archive/2012/11/20 ...
- 【字符串】后缀数组SA
后缀数组 概念 实际上就是将一个字符串的所有后缀按照字典序排序 得到了两个数组 \(sa[i]\) 和 \(rk[i]\),其中 \(sa[i]\) 表示排名为 i 的后缀,\(rk[i]\) 表示后 ...
- [luogu] P3809 【模板】后缀排序 (SA)
板子,照着题解打的倍增版. #include <iostream> #include <cstdio> #include <cstring> using names ...
- 【MVC】ASP.NET MVC Forms验证机制
http://www.cnblogs.com/bomo/p/3309766.html 随笔 - 121 文章 - 0 评论 - 92 [MVC]ASP.NET MVC Forms验证机制 ASP. ...
随机推荐
- Hive[6] HiveQL 查询
6.1 SELECT ... FROM 语句 hive> SELECT name,salary FROM employees; --普通查询 hive>SELECT e.n ...
- view的superview的变换
今天遇到一个奇怪的问题,一个view(称为subview)被加在了一个cell(superView1)上,然后创建了一个view(为superView2),将subview重新加在了superView ...
- 服务器操作nginx相关操作命令
服务器操作nginx相关操作命令 登录服务器: ssh root@0.0.0.0 -p 22100 启动nginx: /usr/local/nginx/sbin/nginx 查看nginx是否启动 p ...
- 在线代码编辑器 Codemirror 的轻量级 React 组件
代码编辑器 CodeMirror 的轻量级 React 组件 demo @uiw-react.github.io/react-codemirror/ 特性:
- spring-AspectJ
动态代理 ProxyFactoryBean织入切面数量太多不利于围护 BeanNameAutoProxyCreater-------------根据Bean名称创建代理 DefaultAdvisorA ...
- scrapy--Cookies
大家好,之前看到的关于cookies的应用,由于有段时间没看,再看的时候花了一些时间,来给大家总结下.本文是根据:"http://www.bubuko.com/infodetail-2233 ...
- php 删除指定扩展名文件
<?php /** *@param $path文件夹绝对路径 $file_type待删除文件的后缀名 *return void */ function clearn_file($path, $f ...
- B1005 继续(3n+1)猜想 (25分)
B1005 继续(3n+1)猜想 (25分) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程 ...
- CSS3---混合模式
概念 CSS3混合模式( CSS Blend Modes )是CSS3新增的一个魔法特性,可以允许多个背景或多个元素进行混合,类似于Photoshop的图层混合模式 CSS3混合模式属性一览 ba ...
- 1008: [HNOI2008]越狱(计数问题)
1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 11361 Solved: 4914[Submit][Status ...