您的位置:首页 > 产品设计 > UI/UE

[POJ3080] Blue Jeans

2015-12-16 14:50 330 查看

传送门

http://poj.org/problem?id=3080

题目大意

给定n个串,询问最长公共字串,长度>=3,长度相同为字典序最小

题解

KMP+暴力枚举

var
x:array[0..20]of string;
next:array[0..100]of longint;
i,j,k,l,o,p,t,tt,ttt:longint;
n,m,anss:longint;
a,ans:string;
begin
readln(t);
for l:=1 to t do
begin
readln(n);
for i:=1 to n do
readln(x[i]);
ans:='no significant commonalities'; anss:=0;
for o:=3 to 60 do
for p:=1 to 60-o+1 do
begin
a:=copy(x[1],p,o); m:=length(a);
next[1]:=0; j:=0;
for i:=2 to m do
begin
while (j>0)and(a[j+1]<>a[i]) do j:=next[j];
if a[j+1]=a[i] then inc(j);
next[i]:=j;
end;
tt:=1;
for k:=2 to n do
begin
j:=0; ttt:=0;
for i:=1 to 60 do
begin
while (j>0)and(x[k][i]<>a[j+1]) do j:=next[j];
if x[k][i]=a[j+1] then inc(j);
if j=m then begin ttt:=1; break; end;
end;
if ttt=0 then begin tt:=0; break; end;
end;
if (tt=1)and((anss<o)or(anss=o)and(a<ans)) then begin ans:=a; anss:=o; end;
end;
writeln(ans);
end;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: