Links: http://www.spoj.com/problems/LCS/
Test instructions two strings of LCS
There's really nothing to say, the first time. Let's turn over the online tutorial to see it again.
Another found that Baidu internal users to communicate the use of pictures in the Baidu snapshot.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineMAXN 2510000#defineMaxt maxn*2structsam_node{intPnt,len; intnxt[ -]; voidPrint (intid=-1) {printf ("-----------------\ n"); printf ("Sam id:%d\n", id); printf ("Next: \ n"); for(intI=0;i< -; i++) printf ("[%c:%d];", i+'a', Nxt[i]); printf ("\ n"); printf ("parent:%d\n", PNT); printf ("length:%d\n", Len); }}SAM[MAXT];intlast=1;inttopt=1;voidAdd_item (Charch) { intP,NP; P=Last ; NP=++topt; Sam[np].len=sam[p].len+1; while(P &&!sam[p].nxt[ch-'a']) Sam[p].nxt[ch-'a']=np,p=Sam[p].pnt; if(!p) {SAM[NP].PNT=1; Last=NP; }Else { intq=sam[p].nxt[ch-'a']; if(sam[q].len==sam[p].len+1) Sam[np].pnt=Q; Else { intNQ; NQ=++topt; SAM[NQ]=Sam[q]; Sam[nq].len=sam[p].len+1; SAM[NQ].PNT=Sam[q].pnt; SAM[Q].PNT=NQ; SAM[NP].PNT=NQ; while(P && sam[p].nxt[ch-'a']==q) {sam[p].nxt[ch-'a']=NQ; P=Sam[p].pnt; }} last=NP; }}CharSTR[MAXN];CharSTR2[MAXN];intMain () {Freopen ("Input.txt","R", stdin); scanf ("%s", str); intn=strlen (str); for(intI=0; i<n;i++) Add_item (Str[i]); for(intI=0; i<=topt;i++) { //Sam[i]. Print (i);} scanf ("%s", STR2); intnow=1, res=0, ans=0; intm=strlen (STR2); for(intI=0; i<m;i++) { if(sam[now].nxt[str2[i]-'a']) { now=sam[now].nxt[str2[i]-'a']; Res++; Ans=Max (ans,res); }Else { while(Now &&!sam[now].nxt[str2[i]-'a']) now=Sam[now].pnt; if(!Now ) { Now=1; Res=0; } Else{res=sam[now].len+1; now=sam[now].nxt[str2[i]-'a']; Ans=Max (ans,res); }}} printf ("%d\n", ans);}
Spoj LCS suffix Automaton