【POJ3294】Life Forms(后缀数组,二分)

时间:2023-03-10 03:31:38
【POJ3294】Life Forms(后缀数组,二分)

题意:【POJ3294】Life Forms(后缀数组,二分)

n<=100 len[i]<=1000

思路:这是一道论文题

【POJ3294】Life Forms(后缀数组,二分)

 var a,x,y,sa,rank,height,wc,wd,ans,flag,b:array[..]of longint;
ch:array[..]of ansistring;
n,n1,l,r,mid,last,i,j,m,len:longint; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; function cmp(a,b,l:longint):boolean;
begin
exit((y[a]=y[b])and(y[a+l]=y[b+l]));
end; procedure getsa(n:longint);
var i,j,p:longint;
begin
for i:= to n- do
begin
x[i]:=a[i];
inc(wc[x[i]]);
end;
for i:= to m- do wc[i]:=wc[i-]+wc[i];
for i:=n- downto do
begin
dec(wc[x[i]]);
sa[wc[x[i]]]:=i;
end;
j:=; p:=;
while p<n do
begin
p:=;
for i:=n-j to n- do
begin
y[p]:=i; inc(p);
end;
for i:= to n- do
if sa[i]>=j then begin y[p]:=sa[i]-j; inc(p); end;
for i:= to n- do wd[i]:=x[y[i]];
for i:= to m- do wc[i]:=;
for i:= to n- do inc(wc[wd[i]]);
for i:= to m- do wc[i]:=wc[i-]+wc[i];
for i:=n- downto do
begin
dec(wc[wd[i]]);
sa[wc[wd[i]]]:=y[i];
end;
for i:= to n do swap(x[i],y[i]);
p:=; x[sa[]]:=;
for i:= to n- do
if cmp(sa[i-],sa[i],j) then x[sa[i]]:=p-
else begin x[sa[i]]:=p; inc(p); end;
j:=j*;
m:=p;
end;
end; procedure getheight(n:longint);
var i,j,k:longint;
begin
for i:= to n do rank[sa[i]]:=i;
k:=;
for i:= to n- do
begin
if k> then dec(k);
j:=sa[rank[i]-];
while a[i+k]=a[j+k] do inc(k);
height[rank[i]]:=k;
end;
end; function isok(x:longint):boolean;
var i,j,cnt,s:longint;
begin
for i:= to n1 do flag[i]:=;
cnt:=; s:=;
for i:= to n do
if height[i]>=x then
begin
cnt:=cnt+flag[b[sa[i]]]; flag[b[sa[i]]]:=;
cnt:=cnt+flag[b[sa[i-]]]; flag[b[sa[i-]]]:=;
end
else
begin
if cnt>n1 div then begin inc(s); ans[s]:=sa[i-]; end;
cnt:=;
for j:= to n1 do flag[j]:=;
end;
if cnt>n1 div then begin inc(s); ans[s]:=sa[n]; end;
if s> then
begin
ans[]:=s;
exit(true);
end;
exit(false);
end; procedure init;
var i:longint;
begin
{for i:=0 to n+1 do
begin
a[i]:=0; x[i]:=0; y[i]:=0;
sa[i]:=0; rank[i]:=0; ans[i]:=0;
wc[i]:=0; wd[i]:=0; b[i]:=0;
height[i]:=0;
end; }
fillchar(a,sizeof(a),);
fillchar(b,sizeof(b),);
fillchar(x,sizeof(x),);
fillchar(y,sizeof(y),);
fillchar(sa,sizeof(sa),);
fillchar(rank,sizeof(rank),);
fillchar(ans,sizeof(ans),);
fillchar(wc,sizeof(wc),);
fillchar(wd,sizeof(wd),);
fillchar(height,sizeof(height),);
end; begin
assign(input,'poj3294.in'); reset(input);
assign(output,'poj3294.out'); rewrite(output);
while not eof do
begin
init;
readln(n1);
if n1= then break;
n:=;
for i:= to n1 do
begin
readln(ch[i]);
len:=length(ch[i]);
for j:=n to n+len- do
begin
a[j]:=ord(ch[i][j-n+])+;
b[j]:=i;
end;
n:=n+len; a[n]:=i;
inc(n);
end;
dec(n); a[n]:=; m:=;
getsa(n+);
getheight(n); l:=; r:=n; last:=;
while l<=r do
begin
mid:=(l+r)>>;
if isok(mid) then begin last:=mid; l:=mid+; end
else r:=mid-;
end;
if last= then writeln('?')
else
begin
for i:= to ans[] do
begin
for j:=ans[i] to ans[i]+last- do write(chr(a[j]-));
writeln;
end;
end;
writeln;
end;
close(input);
close(output);
end.