The longest common substring of a two string.
Stitching two strings separated by special characters, Max (Height[i]) (2<=i<=len,suffix (sa[i]) and suffix (sa[i-1]) are the suffixes of two strings respectively.
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 #defineMAXN 2222227 intWA[MAXN],WB[MAXN],WV[MAXN],WS[MAXN];8 intcmpint*r,intAintBintl) {9 returnR[A]==R[B] && r[a+l]==r[b+l];Ten } One intSA[MAXN],RANK[MAXN],HEIGHT[MAXN]; A voidSA (int(RNintNintm) { - int*x=wa,*y=WB; - the for(intI=0; i<m; ++i) ws[i]=0; - for(intI=0; i<n; ++i) ++ws[x[i]=R[i]]; - for(intI=1; i<m; ++i) ws[i]+=ws[i-1]; - for(inti=n-1; i>=0; -I.) sa[--ws[x[i]]]=i; + - intp=1; + for(intj=1; p<n; j<<=1, m=p) { Ap=0; at for(intI=n-j; i<n; ++i) y[p++]=i; - for(intI=0; i<n; ++i)if(SA[I]>=J) y[p++]=sa[i]-J; - for(intI=0; i<n; ++i) wv[i]=X[y[i]]; - for(intI=0; i<m; ++i) ws[i]=0; - for(intI=0; i<n; ++i) + +Ws[wv[i]]; - for(intI=1; i<m; ++i) ws[i]+=ws[i-1]; in for(inti=n-1; i>=0; -I.) sa[--ws[wv[i]]]=Y[i]; -Swap (x, y); x[sa[0]]=0; p=1; to for(intI=1; i<n; ++i) x[sa[i]]=cmp (y,sa[i-1],sa[i],j)? p1:p + +; + } - the for(intI=1; i<n; ++i) rank[sa[i]]=i; * intk=0; $ for(intI=0; i<n-1; height[rank[i++]]=k) {Panax Notoginseng if(k)--K; - for(intj=sa[rank[i]-1]; R[I+K]==R[J+K]; ++k); the } + } A the intR[MAXN]; + CharSTR[MAXN]; - intMain () { $ intn=0, M; $scanf"%s", str); - for(intI=0; Str[i]; ++i) r[n++]=str[i]-'a'+1; -m=N; ther[n++]= -; -scanf"%s", str);Wuyi for(intI=0; Str[i]; ++i) r[n++]=str[i]-'a'+1; ther[n]=0; -SA (r,n+1, -); Wu intres=0; - for(intI=2; i<=n; ++i) { About if(sa[i]>m&&sa[i-1]<m | | sa[i]<m&&sa[i-1]>M) res=Max (res,height[i]); $ } -printf"%d", res); - return 0; -}
POJ2774 Long long Message (suffix array)