It's so hard to understand, Qaq.
WIKIOI3160 to find the longest common substring of two strings
See Cljppt
1 CharS[MAXN];2 structSam3 {4 intn,last,cnt;5 intgo[maxn][ -],L[MAXN],FA[MAXN];6 voidAddintx)7 {8 intp=last,np=last=++cnt;l[np]=l[p]+1;9 for(;p &&!go[p][x];p =fa[p]) go[p][x]=NP;Ten if(!p) fa[np]=1; One Else A { - intq=Go[p][x]; - if(l[p]+1==L[Q]) fa[np]=Q; the Else - { - intnq=++cnt;l[nq]=l[p]+1; -memcpy (Go[nq],go[q],sizeof(Go[q])); +fa[nq]=Fa[q]; -fa[np]=fa[q]=NQ; + for(;p &&go[p][x]==q;p=fa[p]) go[p][x]=NQ; A } at } - } - voidInit () - { -Last=cnt=1; -scanf"%s", s);intm=strlen (s); inFor0 (i,m-1) Add (s[i]-'a'); - } to voidSolve () + { -scanf"%s", s);intM=strlen (s), now=1, t=0, ans=0; theFor0 (i,m-1) * { $ intx=s[i]-'a';Panax Notoginseng if(Go[now][x]) t++,now=Go[now][x]; - Else the { + while(Now&&!go[now][x]) now=Fa[now]; A if(!now) t=0, now=1; the Elset=l[now]+1, now=Go[now][x]; + } -ans=Max (ans,t); $ } $printf"%d\n", ans); - } - }t; the intMain () - {Wuyi t.init (); the t.solve (); - return 0; Wu}
View Code
Bzoj2555:substring
The positive solution is SAM+LCT, but the subject is obviously no card violence ... wrote a violent result and ran away. RANK4 233
Made an SB mistake is not updated Lastqaq
1 Chars[2*MAXN];2 intMask;3 structSam4 {5 intlast,cnt,fa[maxn],go[maxn][ -],L[MAXN],R[MAXN];6Sam () {last=cnt=1;}7 voidAddintx)8 {9 intp=last,np=++cnt;last=np;l[np]=l[p]+1;Ten for(;p &&!go[p][x];p =fa[p]) go[p][x]=NP; One if(!p) fa[np]=1; A Else - { - intq=Go[p][x]; the if(l[p]+1==L[Q]) fa[np]=Q; - Else - { - intnq=++cnt;l[nq]=l[p]+1; +memcpy (Go[nq],go[q],sizeof(Go[q])); -r[nq]=R[q]; +fa[nq]=Fa[q]; Afa[q]=fa[np]=NQ; at for(;p &&go[p][x]==q;p=fa[p]) go[p][x]=NQ; - } - } - for(; NP;NP=FA[NP]) r[np]++; - } - voidInsertChars[]) in { - intN=strlen (s), t=Mask; toFor0 (i,n-1) {mask= (mask*131+i)%N;swap (S[i],s[mask]);} +mask=T; -For0 (i,n-1) Add (s[i]-'A'); the } * voidQueryChars[]) $ {Panax Notoginseng intN=strlen (s), now=1, t=Mask; -For0 (i,n-1) {mask= (mask*131+i)%N;swap (S[i],s[mask]);} theFor0 (i,n-1) now=go[now][s[i]-'A']; +mask=t^R[now]; Aprintf"%d\n", R[now]); the } + }t; - intMain () $ { $Freopen ("Input.txt","R", stdin); -Freopen ("output.txt","W", stdout); - intq=read (); thescanf"%s", s);intN=strlen (s); For0 (i,n-1) T.add (s[i]-'A'); - while(q--)Wuyi { thescanf"%s", s); - if(s[0]=='A') {scanf ("%s", s); T.insert (s);} Wu Else{scanf ("%s", s); T.query (s);} - } About return 0; $}
View Code
Automatic suffix machine