题面
https://www.luogu.org/problem/P3804
题解
// luogu-judger-enable-o2 #include<cstdio> #include<iostream> #include<cstring> #define ri register int #define N 1000050 using namespace std; char s[N]; int n; int ans=0; struct SAM{ int ff[N<<1],cnt[N<<1]; int ch[N<<1][26]; int len[N<<1]; int tot,last; int ton[N<<1],a[N<<1]; inline void copy(int nq,int q) { for (ri i=0;i<26;i++) ch[nq][i]=ch[q][i]; ff[nq]=ff[q]; } inline void extend(int o) { int p=last,np,q,nq; np=++tot; len[np]=len[p]+1; last=np; while (p&&!ch[p][o]) ch[p][o]=np,p=ff[p]; if (!p) ff[np]=1; else { q=ch[p][o]; if (len[q]==len[p]+1) ff[np]=q; else { nq=++tot; copy(nq,q); len[nq]=len[p]+1; ff[np]=ff[q]=nq; while (p && ch[p][o]==q) ch[p][o]=nq,p=ff[p]; } } cnt[np]=1; } void work() { for (ri i=1;i<=tot;i++) ton[len[i]]++; for (ri i=1;i<=n;i++) ton[i]+=ton[i-1]; for (ri i=1;i<=tot;i++) a[ton[len[i]]--]=i; for (ri i=tot;i>=1;i--) cnt[ff[a[i]]]+=cnt[a[i]]; for (ri i=1;i<=tot;i++) if (cnt[i]>1) ans=max(ans,len[i]*cnt[i]); } } sam; int main() { scanf("%s",s+1); n=strlen(s+1); sam.tot=1; sam.last=1; for (ri i=1;i<=n;i++) sam.extend(s[i]-'a'); ans=0; sam.work(); printf("%d\n",ans); }