1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; #define ll long long const int N = 1e6+10; char str1[300],res[300]; int t,n,m,rk[N],s[N],tp[N],sa[N],tax[N],id[N],h[N]; bool vis[4444]; bool cmp(int *r,int a,int b,int l) { return r[a]==r[b]&&r[a+l]==r[b+l]; } void da(int str[],int n,int m) { n++; int i,j,p,*x=rk,*y=tp; for(i=0;i<m;i++)tax[i]=0; for(i=0;i<n;i++)tax[x[i]=str[i]]++; for(i=1;i<m;i++)tax[i]+=tax[i-1]; for(i=n-1;i>=0;i--)sa[--tax[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++)tax[i]=0; for(i=0;i<n;i++)tax[x[y[i]]]++; for(i=1;i<m;i++)tax[i]+=tax[i-1]; for(i=n-1;i>=0;i--)sa[--tax[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; } int k=0; n--; for(i=0;i<=n;i++)rk[sa[i]]=i; for(i=0;i<n;i++) { if(k)k--; j=sa[rk[i]-1]; while(str[i+k]==str[j+k])k++; h[rk[i]]=k; } } int check(int k){ int cnt = 0; memset(vis,0,sizeof(vis)); for(int i=2;i<=n;i++){ if(h[i]<k){ 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==t){ for(int j=0;j<k;j++)res[j]=s[sa[i]+j]; res[k]='\0'; return 1; } } return 0; } int main(){ while(~scanf("%d",&t),t){ n=0; int temp = 30; for(int i=0;i<t;i++){ scanf("%s",str1); int len = strlen(str1); for(int j=0;j<len;j++){ s[n]=str1[j]; id[n++]=i; } s[n]='#'+i; id[n++]='#'+i; } s[n]=0; da(s,n,5000); int ans = 0; int l=1,r=strlen(str1); while(l<=r){ int mid = (l+r)>>1; if(check(mid)){ ans=1; l=mid+1; } else r=mid-1; } if(!ans)printf("IDENTITY LOST\n"); else printf("%s\n",res); } return 0; }
|