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')
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
///后缀数组 倍增算法
const int maxn=200000;
char str[maxn];
int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn];
int n;///字符串长度
int cmp(int* r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
/**n为字符串长度,m为字符的取值范围,r为字符串。后面的j为每次排
序时子串的长度*/
void DA(int* r,int* sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
///对R中长度为1的子串进行基数排序
for(i=0;i<m;i++)wn[i]=0;
for(i=0;i<n;i++)wn[x[i]=r[i]]++;
for(i=1;i<m;i++)wn[i]+=wn[i-1];
for(i=n-1;i>=0;i--)sa[--wn[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++)wn[i]=0;
for(i=0;i<n;i++)wn[wv[i]]++;
for(i=1;i<m;i++)wn[i]+=wn[i-1];
for(i=n-1;i>=0;i--)sa[--wn[wv[i]]]=y[i];
///当p=n的时候,说明所有串都已经排好序了
///在第一次排序以后,rank数组中的最大值小于p,所以让m=p
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++;
}
return;
}
///后缀数组 计算height数组
/**
height数组的值应该是从height[1]开始的,而且height[1]应该是等于0的。
原因是,+因为我们在字符串后面添加了一个0号字符,所以它必然是最小的
一个后缀。而字符串中的其他字符都应该是大于0的(前面有提到,使用倍
增算法前需要确保这点),所以排名第二的字符串和0号字符的公共前缀
(即height[1])应当为0.在调用calheight函数时,要注意height数组的范
围应该是[1..n]。所以调用时应该是calheight(r,sa,n)
而不是calheight(r,sa,n+1)。*/
int rank[maxn],height[maxn];
void calheight(int* r,int* sa,int n)
{
int i,j,k=0;
for(i=1;i<=n;i++)rank[sa[i]]=i;
for(i=0;i<n;height[rank[i++]]=k)
for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
return;
}
///求n个字符串的最长公共子串(这里可以是反序相同)
int m;//m个字符串
int len[maxn];//第i个字符串的长度
const int inf=100000000;
int get(int x){
int i,j;
for (i=1,j=len[0];x>j;j+=len[i]+1,i++);
if (x==j) return 0;
else return i;
}
int _check(int k){
int v[maxn]={0},ma[maxn]={-1},_min[maxn]={inf},i,x;
for (i=1;i<=n;i++)
{
if (height[i]<k)
{
int j;
for (j=1;j<=m;j++)
{
if (v[j]<2) {j=0;break;}
if (ma[j]-_min[j]<k) {j=0;break;}
}
if (j==m+1) return 1;
for (j=0;j<=m;j++)
{
ma[j]=-1;
_min[j]=inf;
v[j]=0;
}
}
x=get(sa[i]);
ma[x]=ma[x]>sa[i]?ma[x]:sa[i];
_min[x]=_min[x]<sa[i]?_min[x]:sa[i];
v[x]++;
}
return 0;
}
void solve()//m>2
{
int l=0,r=n;//二分枚举长度
while(l+1<r)
{
int mid=(l+r)>>1;
if(_check(mid)) l=mid;
else r=mid-1;
}
if(_check(r)) l=r;
printf("%d/n",l);
}
int main()
{
//后缀数组 倍增算法 使用方法
/**
在使用倍增算法前,需要保证r数组的值均大于0。然后要在原字
符串后添加一个0号字符,具体原因参见罗穗骞的论文。这时候,
若原串的长度为n,则实际要进行后缀数组构建的r数组的长度应
该为n+1.所以调用DA函数时,对应的n应为n+1.*/
/* int n=strlen(str);//str 待处理字符串
for(int i=0;i<n;i++) a[i]=(int)str[i];
a[n]=0;
DA(a,sa,n+1,256);
calheight(a,sa,n);*/
//....................................
int ci;scanf("%d",&ci);
while(ci--)
{
n=0;//总长度
int k=0;//第k个字符串
scanf("%d",&m);
//将字符串及其反串连接在一起,中间用不同的字符隔开
for(int i=0;i<m;i++)
{
scanf("%s",str);
len[k++]=strlen(str);
for(int j=0;j<len[k-1];j++) a[n++]=(int)str[j];
a[n++]=300+k;
}
a[n]=0;
DA(a,sa,n+1,600);
calheight(a,sa,n);
solve();
}
return 0;
}