Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.
After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.
Your task is to find such a sequence.
Input
The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.
After the last trademark, the next task begins. The last task is followed by a line containing zero.
Output
For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.
Sample Input
3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0
Sample Output
abb
IDENTITY LOST
题目链接:http://poj.org/problem?id=3450
题目大意:有k个字符串,问这k个字符串的最大公共子串
思路:和POJ-2774求两个字符串的最大公共子串差不多。先把所有字符串连在一起,用字符隔开,并标记当前位置属于哪一字符串。二分最大公共子串,判断是否有k个属于不同字符串的连续的hi>=mid
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int N=1000005;
int a[N],id[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 vis[4005],n,m,k;
char b[205],s[205];
int fun(int x)
{
memset(vis,0,sizeof(vis));
int cnt=0;
for(int i=2;i<=n;i++)
{
if(h[i]<x)
{
memset(vis,0,sizeof(vis));
cnt=0;
continue;
}
if(!vis[id[sa[i-1]]]) vis[id[sa[i-1]]]=1,cnt++;
if(!vis[id[sa[i]]]) vis[id[sa[i]]]=1,cnt++;
if(cnt==k)
{
for(int j=0;j<x;j++) b[j]=a[sa[i]+j];
b[x]='\0';
return 1;
}
}
return 0;
}
int main()
{
while(~scanf("%d",&k)&&k)
{
n=0,m=127+k;
for(int i=0;i<k;i++)
{
scanf("%s",s);
int l=strlen(s);
for(int j=0;j<l;j++) a[n]=s[j],id[n++]=i;
a[n]='#'+i,id[n++]='#'+i;
}
a[n]=0;
da(a,sa,rak,h,n,m);
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;
}
if(ans) printf("%s\n",b);
else printf("IDENTITY LOST\n");
}
return 0;
}