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
(in the example above, the longest substring which fulfills the requirements is ‘ba’)
他这个后缀数组是跑到0的位置停下来的,所以如果不memset(a)或者把a[pos]设为0的话就会re,无限re。这道题就是重复旋律2和重复旋律3的组合,只要在check的时候用4个数组记录一下就好了,非常的敢单
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN =110000;
int wa[MAXN],wb[MAXN],wv[MAXN],we[MAXN],rk[MAXN];
int cmp(int *r,int a,int b,int l){return r[a]==r[b]&&r[a+l]==r[b+l];}
void build_sa(int *r,int *sa,int n,int m){
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++)we[i]=0;
for(i=0;i<n;i++)we[x[i]=r[i]]++;
for(i=1;i<m;i++)we[i]+=we[i-1];
for(i=n-1;i>=0;i--)sa[--we[x[i]]]=i;
for(j=1,p=1;p<n;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<n;i++)wv[i]=x[y[i]];
for(i=0;i<m;i++)we[i]=0;
for(i=0;i<n;i++)we[wv[i]]++;
for(i=1;i<m;i++)we[i]+=we[i-1];
for(i=n-1;i>=0;i--)sa[--we[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}
int height[MAXN];
void calheight(int *r,int *sa,int n){
int i,j,k=0;
for(i=1;i<=n;i++)rk[sa[i]]=i;
for(i=0;i<n;height[rk[i++]]=k){
for(k?k--:0,j=sa[rk[i]-1];r[i+k]==r[j+k];k++);
}
}
int sa[MAXN],a[MAXN];
int n,k,id[MAXN],pos;
int maxn[MAXN],minn[MAXN],st[MAXN],vis[MAXN];
void upmax(int &a,int b)
{
if(a==-1||a<b)
a=b;
}
void upmin(int &a,int b)
{
if(a==-1||a>b)
a=b;
}
bool check(int K)
{
memset(maxn,-1,sizeof(maxn));
memset(minn,-1,sizeof(minn));
memset(vis,0,sizeof(vis));
int ans=0,top=0;
for(int i=1;i<=pos;i++)
{
if(height[i]< K)
{
ans=0;
while(top>=0)
{
int v=st[top];
maxn[v]=minn[v]=-1;
vis[v]=0;
top--;
}
upmax(maxn[id[sa[i]]],sa[i]);
upmin(minn[id[sa[i]]],sa[i]);
st[++top]=id[sa[i]];
}
else
{
upmax(maxn[id[sa[i]]],sa[i]);
upmin(minn[id[sa[i]]],sa[i]);
st[++top]=id[sa[i]];
if(maxn[id[sa[i]]]-minn[id[sa[i]]]>=K)
{
if(!vis[id[sa[i]]])
ans++;
vis[id[sa[i]]]=1;
if(ans>=n)
return true;
}
}
}
return false;
}
char s[MAXN];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(sa,0,sizeof(sa));
scanf("%d",&n);
pos=0;
for(int i=0;i<n;i++)
{
scanf("%s",s+pos);
while(s[pos]!='\0')
id[pos]=i,a[pos]=s[pos]-'a'+1,pos++;
}
a[pos]=0;
build_sa(a,sa,pos+1,200);
calheight(a,sa,pos);
int low=0,high=pos,mid,ans;
while(high>=low)
{
mid=low+high>>1;
if(check(mid))
ans=mid,low=mid+1;
else
high=mid-1;
}
printf("%d\n",ans);
}
return 0;
}