Problem: The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days:his mother is getting ill. Being worried about spending so much in railway tickets (Byterland is such a big country, and he have to spend all shours on Train to him hometown), he decided only to send SMS with his mother.
The little cat lives in a Unrich family, so he frequently comes to the Mobile service center, to check how much money he Have spent on SMS. Yesterday, the computer of service center was broken, and printed, very long messages. The brilliant little cat soon found out:
1. All characters in messages is lowercase Latin letters, without punctuations and spaces.
2. All SMS have been appended other– (i+1)-th SMS comes directly after the i-th one–that are why those Es is quite long.
3. His own SMS have been appended together, but possibly a great many redundancy characters appear leftwards and rightwards Due to the broken computer.
E.g:if his SMS was "motheriloveyou", either long message printed by, the would possibly be one of "Hahamotherilov Eyou "," motheriloveyoureally "," Motheriloveyouornot "," bbbmotheriloveyouaaa ", etc.
4. For these broken issues, the little cat have printed his original text twice (so there appears, very long messages). Even though the original text remains the same in both printed messages, the redundancy characters on both sides would is P Ossibly different.
You are given those-very long messages, and you had to output the length of the longest possible original text Writte n by the little cat.
Background:
The SMS in Byterland Mobile service is charging in Dollars-per-byte. That's why the little cat was worrying about how long could the longest original text being.
Why do I ask you to write a program? There is four resions:
1. The little cat is so busy these days with physics lessons;
2. The little cat wants to keep what he said to his mother Seceret;
3. POJ is such a great Online Judge;
4. The little cat wants to earn some money from POJ, and try to persuade he mother to see the Doctor:( Inputtwo strings with lowercase letters on both of the input lines individually. Number of characters in each one would never exceed 100000.OutputA single line with a single integer Number–what is the M Aximum length of the original text written by the little cat. Sample Input
Yeshowmuchiloveyoumydearmotherreallyicannotbelieveityeaphowmuchiloveyoumydearmother
Sample Output
27
Test instructions
The longest common string of two strings.
Ideas:
suffix automata: can be directly matched, and then the dictation of the suffix machine.
#include <cstdio>#include<cstdlib>#include<iostream>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=3000000; CharCHR[MAXN],STR[MAXN];structsam{intch[maxn][ -],fa[maxn],maxlen[maxn],last,sz; voidinit () {sz=last=1; fa[1]=maxlen[1]=0; memset (ch[1],0,sizeof(ch[1])); } 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])); 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]; } } } voidsolve () {scanf ("%s", CHR); intL=strlen (CHR), x,tmp=0, ans=0; last=1; for(intI=0; i<l;i++) {x=chr[i]-'a'; if(Ch[last][x]) tmp++,last=Ch[last][x]; Else { while(Last&&!ch[last][x]) last=Fa[last]; if(! Last) tmp=0, last=1; Elsetmp=maxlen[last]+1, last=Ch[last][x]; } ans=Max (ans,tmp); } printf ("%d\n", ans); }}; Sam Sam;intMain () {intt,i,l; while(~SCANF ("%s", CHR)) {Sam.init (); L=strlen (CHR); for(i=0; i<l;i++) Sam.add (chr[i]-'a'); Sam.solve (); } return 0;}
View Code
suffix array: two strings need to be concatenated, plus a special symbol, to ensure that the resulting results conform to the two strings from a different parent string.
Efficiency comparison:
Suffix automaton 94ms, suffix array 782ms. I'm still willing to write a suffix automaton, but it's always good to practice.
#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn=4100000;CharSTR1[MAXN],STR2[MAXN]; intL,CH[MAXN];structsa{intCNTA[MAXN],CNTB[MAXN],A[MAXN],B[MAXN]; intRANK[MAXN],SA[MAXN],TSA[MAXN],HT[MAXN]; voidsort () { for(inti =0; I < -; i + +) cnta[i] =0; for(inti =1; I <= L; i + +) Cnta[ch[i]] + +; for(inti =1; I < -; i + +) Cnta[i] + = cnta[i-1]; for(inti = L; I I--) sa[cnta[ch[i]]---] =i; rank[sa[1]] =1; for(inti =2; I <= L; i + +) {Rank[sa[i]]= Rank[sa[i-1]]; if(Ch[sa[i]]! = ch[sa[i-1]]) Rank[sa[i]] + +; } for(intL =1; Rank[sa[l]] < L; L <<=1){ for(inti =0; I <= L; i + +) cnta[i] =0; for(inti =0; I <= L; i + +) cntb[i] =0; for(inti =1; I <= L; i + +) {Cnta[a[i]= Rank[i]] + +; Cntb[b[i]= (i + L <= L)? Rank[i + L]:0] ++; } for(inti =1; I <= L; i + +) Cntb[i] + = cntb[i-1]; for(inti = L; I I--) tsa[cntb[b[i]]---] =i; for(inti =1; I <= L; i + +) Cnta[i] + = cnta[i-1]; for(inti = L; I I--) sa[cnta[a[tsa[i] []---] =Tsa[i]; rank[sa[1]] =1; for(inti =2; I <= L; i + +) {Rank[sa[i]]= Rank[sa[i-1]]; if(A[sa[i]]! = a[sa[i-1]] || B[sa[i]]! = b[sa[i-1]]) Rank[sa[i]] + +; } } } voidgetht () { for(inti =1, j =0; I <= L; i + +){ if(j) J--; while(Ch[i + j] = = Ch[sa[rank[i]-1] + j]) J + +; Ht[rank[i]]=J; }}};sa SA;intMain () {scanf ("%s", str1+1); scanf ("%s", str2+1); intL1=strlen (str1+1); intL2=strlen (str2+1); for(intI=1; i<=l1;i++) ch[i]=str1[i]-' a ' +1; CH[L1+1]= -; for(intI=1; i<=l2;i++) ch[i+l1+1]=str2[i]-' A ' +1; L=l1+l2+1; Sa.sort (); SA.GETHT (); intans=0; for(inti =1; I <= L; i++) { if(SA.SA[I]<=L1)! = (sa.sa[i-1]<=L1)) Ans=Max (ans, sa.ht[i]); } printf ("%d\n", ans); return 0;}
Poj2774long Long Message (suffix array & suffix automaton)