# spoj220求至少出现两次且不重叠的最长公共子串

1274人阅读 评论(0)

You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to make this fragment as long as possible.

### Input

The first line of input contains a single positive integer t<=10, the number of test cases. t test cases follow. Each test case begins with integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters 'a'-'z', possibly with some additional trailing white space which should be ignored.

### Output

For each test case output the length of longest string which appears disjointly at least twice in all of the messages.

### Example

Input:
1
4
abbabba
dabddkababa
bacaba
baba

Output:
2
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=1000000+10;
int *rank,r[MAX],sa[MAX],height[MAX],n;
int wa[MAX],wb[MAX],wm[MAX],pos[15],xy[15][2];
char s[MAX/10];

bool cmp(int *r,int a,int b,int l){
return r[a] == r[b] && r[a+l] == r[b+l];
}

void makesa(int *r,int *sa,int n,int m){
int *x=wa,*y=wb,*t;
for(int i=0;i<m;++i)wm[i]=0;
for(int i=0;i<n;++i)wm[x[i]=r[i]]++;
for(int i=1;i<m;++i)wm[i]+=wm[i-1];
for(int i=n-1;i>=0;--i)sa[--wm[x[i]]]=i;
for(int i=0,j=1,p=0;p<n;j=j*2,m=p){
for(p=0,i=n-j;i<n;++i)y[p++]=i;
for(i=0;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=0;i<m;++i)wm[i]=0;
for(i=0;i<n;++i)wm[x[y[i]]]++;
for(i=1;i<m;++i)wm[i]+=wm[i-1];
for(i=n-1;i>=0;--i)sa[--wm[x[y[i]]]]=y[i];
for(t=x,x=y,y=t,x[sa[0]]=0,i=p=1;i<n;++i){
x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
}
}
rank=x;
}

void calheight(int *r,int *sa,int n){
for(int i=0,j=0,k=0;i<n;height[rank[i++]]=k){
for(k?--k:0,j=sa[rank[i]-1];r[i+k] == r[j+k];++k);
}
}

void check(int k){
int i=0;
for(i=0;i<=n;++i)if(k<pos[i])break;
xy[i][0]=min(xy[i][0],k);
if(xy[i][0] == -1)xy[i][0]=k;
xy[i][1]=max(xy[i][1],k);
}

bool panduan(int k){
int i=0;
for(i=0;i<n;++i)if(xy[i][1]-xy[i][0]<k)return false;
return true;
}

int main(){
int t;
cin>>t;
while(t--){
cin>>n;
int len=-1,j=0;
for(int i=0;i<n;++i){
cin>>s;
for(j=0,++len;s[j] != '\0';++j,++len)r[len]=s[j];
r[len]=300+i;
pos[i]=len;
}
r[len]=0;
makesa(r,sa,len+1,310);
calheight(r,sa,len);
int l=0,r=len,mid;
while(l<=r){
mid=l+r>>1;
memset(xy,-1,sizeof xy);
for(int i=1;i<=len;++i){
if(height[i]>=mid){
check(sa[i]);
check(sa[i-1]);
}else{
if(panduan(mid))break;
memset(xy,-1,sizeof xy);
}
}
if(panduan(mid))l=mid+1;
else r=mid-1;
}
cout<<r<<endl;
}
return 0;
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：401670次
• 积分：7129
• 等级：
• 排名：第3147名
• 原创：312篇
• 转载：2篇
• 译文：0篇
• 评论：101条
评论排行
最新评论