后缀自动机板子题
https://vjudge.net/problem/28017/origin
找多串的最长公共子串
//#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define mod (1000000007) #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pii pair<int,int> #define ull unsigned long long #define base 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-12; const int N=500000+10,maxn=1200000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; char s[N]; int c[N],a[N]; struct SAM{ int last,cnt; int ch[N<<1][26],fa[N<<1],l[N<<1]; int mx[N<<1],tmp[N<<1]; void ins(int c){ int p=last,np=++cnt;last=np;l[np]=l[p]+1; for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np; if(!p)fa[np]=1; else { int q=ch[p][c]; if(l[p]+1==l[q])fa[np]=q; else { int nq=++cnt;l[nq]=l[p]+1; memcpy(ch[nq],ch[q],sizeof(ch[q])); fa[nq]=fa[q];fa[q]=fa[np]=nq; for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq; } } } void build(){ int len=strlen(s+1); last=cnt=1; for(int i=1;i<=len;i++)ins(s[i]-'a'); topo(); for(int i=1;i<=cnt;i++)mx[i]=l[i],tmp[i]=0; } void topo(){ for(int i=1;i<=cnt;i++)c[l[i]]++; for(int i=1;i<=cnt;i++)c[i]+=c[i-1]; for(int i=1;i<=cnt;i++)a[c[l[i]]--]=i; } void debug() { puts(""); for(int i=1;i<=cnt;i++) { printf("%d ",i); for(int j=0;j<26;j++) printf("%d ",ch[i][j]); printf("%d %d\n",fa[i],l[i]); } } void faupdate() { for(int i=cnt;i;i--) tmp[fa[a[i]]]=max(tmp[fa[a[i]]],tmp[a[i]]); } void maupdate() { for(int i=1;i<=cnt;i++) mx[i]=min(mx[i],tmp[i]),tmp[i]=0; } void match() { int len=strlen(s+1),val=0,now=1,res=0; for(int i=1;i<=len;i++) { if(ch[now][s[i]-'a']) { now=ch[now][s[i]-'a']; res++; } else { while(now&&!ch[now][s[i]-'a'])now=fa[now]; if(!now)now=1,res=0; else { res=l[now]+1; now=ch[now][s[i]-'a']; } } tmp[now]=max(tmp[now],res); } faupdate(); maupdate(); } int maxlen() { int ans=0; for(int i=1;i<=cnt;i++)ans=max(ans,mx[i]); return ans; } }sam; int main() { scanf("%s",s+1); sam.build(); while(~scanf("%s",s+1)) sam.match();//,printf("%d\n",sam.maxlen()); printf("%d\n",sam.maxlen()); return 0; } /******************** ********************/