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')
题目链接:https://vjudge.net/problem/SPOJ-PHRASES
题目大意:给出k个字符串,问每个串中至少出现两次且不重叠的最长公共子串
思路:后缀数组模板求出sa,height 数组,二分最长公共子串长度
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int N=200005;
int a[N],sa[N],rak[N],h[N],c[N],t1[N],t2[N];
bool cmp(int *f,int x,int y,int w){return f[x]==f[y]&&f[x+w]==f[y+w];}
void da(int a[], int sa[], int rak[], int h[], int n, int m)
{
n++;
int i,j,p,*x = t1,*y = t2;
for (i=0; i<m; i++) c[i]=0;
for (i=0; i<n; i++) c[x[i]=a[i]]++;
for (i=1; i<m; i++) c[i]+=c[i-1];
for (i=n-1; i>=0; i--) sa[--c[x[i]]]=i;
for (j=1; j<=n; j<<=1)
{
p=0;
for (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++) c[i]=0;
for (i=0; i<n; i++) c[x[y[i]]]++;
for (i=1; i<m; i++) c[i]+=c[i-1];
for (i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i];
swap(x, y); p=1; x[sa[0]]=0;
for (i=1; i<n; i++) x[sa[i]]=cmp(y, sa[i-1], sa[i], j)?p-1:p++;
if(p>=n) break;
m=p;
}
n--;
for(i=0;i<=n;i++) rak[sa[i]]=i;
int k=0;
for(int i=0;i<n;h[rak[i++]]=k)
for(k=k?k-1:k,j=sa[rak[i]-1];a[i+k]==a[j+k];k++);
}
int n,k,minn[50],maxx[50],id[N];
char s[N];
int fun(int x)
{
int j;
memset(minn,inf,sizeof(minn));
memset(maxx,-1,sizeof(maxx));
for(int i=1;i<=n;i++)
{
if(h[i]<x)
{
memset(minn,inf,sizeof(minn));
memset(maxx,-1,sizeof(maxx));
}
else
{
maxx[id[sa[i]]]=max(maxx[id[sa[i]]],sa[i]);
minn[id[sa[i]]]=min(minn[id[sa[i]]],sa[i]);
maxx[id[sa[i-1]]]=max(maxx[id[sa[i-1]]],sa[i-1]);
minn[id[sa[i-1]]]=min(minn[id[sa[i-1]]],sa[i-1]);
for(j=0;j<k;j++)
if(maxx[j]-minn[j]<x) break;
if(j==k) return 1;
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
n=0;
scanf("%d",&k);
for(int i=0;i<k;i++)
{
scanf("%s",s);
int len=strlen(s);
for(int j=0;j<len;j++)a[n]=s[j],id[n++]=i;
a[n]=i;id[n++]=i;
}
a[n]=0,id[n]=0;
da(a,sa,rak,h,n,127+k);
int l=0,r=strlen(s),ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(fun(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}