Beside other services, ACM helps companies-clearly state their ' corporate identity ', which includes company logos but AL So and signs, like trademarks. One of such companies is Internet Building Masters (IBM), which have recently asked ACM for a to help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers is used to the old one S. Therefore, ACM would only change existing trademarks instead of creating new ones.
After several other proposals, it is decided to take all existing trademarks and find the longest common sequence of Lett ERS is contained in all of them. This sequence is graphically emphasized to form a new logo. Then, the old trademarks may still is 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 and each containing one trademark. Trademarks'll is composed only from lowercase letters, the length of each trademark would be at least 1 and at most C Haracters.
After the last trademark, the next task begins. The last task was 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 is several strings of the same length, print the one and that is lexicographically smallest. If There is no such non-empty string, output the words "IDENTITY LOST" instead.
Sample Input
3aabbaabbabbababbbbbbbabb2xyzabc0
Sample Output
Abbidentity LOST
Test instructions
The longest common string of n strings, if there is more than one, the output of the smallest dictionary order.
Ideas:
kmp| | Suffix Array | | Generalized suffix automata, no, it's high school. Code is more violent. After class, try the optimization again.
Contrast:
The suffix array sa has been sorted, and the smallest dictionary order is easy to find. The suffix automaton needs to be searched like a dictionary tree.
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<memory>#include<cmath>using namespacestd;intN,len,ans,max,now;Const intmaxn=4000010; Chars[2100],cap[2100];structsam{intch[maxn][ -],fa[maxn],maxlen[maxn],last,sz; intROOT,NXT[MAXN],SIZE[MAXN];BOOLFlag; voidinit () {sz=0; flag=false; Root=++sz; memset (Size,0,sizeof(size)); memset (ch[1],0,sizeof(ch[1])); memset (NXT,0,sizeof(NXT)); } voidAddintx) {intNp=++sz,p=last; last=NP; memset (CH[NP],0,sizeof(CH[NP])); MAXLEN[NP]=maxlen[p]+1; while(P&&!ch[p][x]) ch[p][x]=np,p=Fa[p]; if(!p) fa[np]=1; Else { intq=Ch[p][x]; if(maxlen[p]+1==MAXLEN[Q]) fa[np]=Q; Else { intnq=++sz; memcpy (Ch[nq],ch[q],sizeof(Ch[q])); SIZE[NQ]=SIZE[Q]; nxt[nq]=Nxt[q]; MAXLEN[NQ]=maxlen[p]+1; FA[NQ]=Fa[q]; FA[Q]=fa[np]=NQ; while(p&&ch[p][x]==q) ch[p][x]=nq,p=Fa[p]; } } for(; np;np=FA[NP])if(nxt[np]!=Now ) {SIZE[NP]++; NXT[NP]=Now ; }Else Break; } /*void dfs (int x,int d) {//Output if (flag| | D>n) return; if (d==n) {puts (CAP); Flag=true; Return } for (int i=0;i<26;i++) if (ch[x][i]&&size[ch[x][i]]==n) {cap[d]=i+ ' a '; Dfs (ch[x][i],d+1); cap [D]=0; } }*/ voidDfsintXintD) {//Output if(d!=maxlen[x]| | d>ans| | Flag)return; if(maxlen[x]==ans&&size[x]>=n) {puts (CAP); flag=true;return; } for(intI=0;i< -;++i)if(Ch[x][i]) {cap[d]=i+'a'; DFS (ch[x][i],d+1); cap[d]=0; } }}; Sam Sam;intMain () { while(~SCANF ("%d", &n) &&N) {Sam.init (); for(intI=1; i<=n;i++) {scanf ("%s", s+1); Sam.last=Sam.root; Len=strlen (s+1); now=i; for(intj=1; j<=len;j++) Sam.add (s[j]-'a'); } ans=0; for(intI=1; i<=sam.sz;i++) if(Sam.size[i]==n&&sam.maxlen[i]>ans) ans=Sam.maxlen[i]; if(ANS) Sam.dfs (1,0); Elseprintf"IDENTITY lost\n"); } return 0;}
POJ3080 poj3450corporate Identity (generalized suffix automata | | Suffix Array | | KMP)