PHRASES - Relevant Phrases of Annihilation
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<algorithm>
#include<cstring>
#define N 200003
using namespace std;
int sa[N],rank[N],height[N],xx[N],yy[N],*x,*y;
int n,m,len,p,vis[N],b[N],a[N],ans,pos[N],mn[N],mx[N];
char s[N];
void init()
{
memset(sa,0,sizeof(sa));
memset(rank,0,sizeof(rank));
memset(height,0,sizeof(height));
memset(b,0,sizeof(b));
memset(vis,0,sizeof(vis));
memset(mn,127/3,sizeof(mn));
memset(mx,0,sizeof(mx));
}
int cmp(int i,int j,int l)
{
return y[i]==y[j]&&(i+l>len?-1:y[i+l])==(j+l>len?-1:y[j+l]);
}
void get_SA()
{
x=xx; y=yy; m=200;
for (int i=1;i<=len;i++) b[x[i]=a[i]]++;
for (int i=1;i<=m;i++) b[i]+=b[i-1];
for (int i=len;i>=1;i--) sa[b[x[i]]--]=i;
for (int k=1;k<=len;k<<=1) {
p=0;
for (int i=len-k+1;i<=len;i++) y[++p]=i;
for (int i=1;i<=len;i++)
if (sa[i]>k) y[++p]=sa[i]-k;
for (int i=1;i<=m;i++) b[i]=0;
for (int i=1;i<=len;i++) b[x[y[i]]]++;
for (int i=1;i<=m;i++) b[i]+=b[i-1];
for (int i=len;i>=1;i--) sa[b[x[y[i]]]--]=y[i];
swap(x,y); p=2; x[sa[1]]=1;
for (int i=2;i<=len;i++)
x[sa[i]]=cmp(sa[i],sa[i-1],k)?p-1:p++;
if (p>len) break;
m=p+1;
}
p=0;
for (int i=1;i<=len;i++) rank[sa[i]]=i;
for (int i=1;i<=len;i++) {
if (rank[i]==1) continue;
int j=sa[rank[i]-1];
while (i+p<=len&&j+p<=len&&a[i+p]==a[j+p]) p++;
height[rank[i]]=p;
p=max(p-1,0);
}
}
int pd(int x)
{
memset(vis,0,sizeof(vis));
memset(mn,127/3,sizeof(mn)); int inf=mn[0];
memset(mx,0,sizeof(mx));
int size=0; int last=1;
mx[pos[sa[1]]]=mn[pos[sa[1]]]=sa[1];
for (int i=2;i<=len;i++)
if (height[i]>=x) {
int t=pos[sa[i]];
vis[t]++;
mx[t]=max(mx[t],sa[i]);
mn[t]=min(mn[t],sa[i]);
}
else {
size=0;
for (int j=last;j<=i-1;j++){
int t=pos[sa[j]];
if (mx[t]-mn[t]>=x)
size++;
mx[t]=0,mn[t]=inf;
}
if (size==n) return 1;
size=0; last=i; mx[pos[sa[i]]]=mn[pos[sa[i]]]=sa[i];
}
return 0;
}
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
int T; scanf("%d",&T);
for (int L=1;L<=T;L++)
{
init();
scanf("%d",&n); len=0;
for (int i=1;i<=n;i++) {
scanf("%s",s+1);
m=strlen(s+1);
if (i!=1) a[++len]=i+27;
for (int j=1;j<=m;j++)
a[++len]=s[j]-'a'+1,pos[len]=i;
}
get_SA();
int l=1; int r=len; int ans=0;
while (l<=r) {
int mid=(l+r)/2;
if (pd(mid)) ans=max(ans,mid),l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
}