AC自动机的fail可以成树 KMP的next也能成树
这道题就是沿着树往上统计出现次数
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=100005;
int n; char S[N];
int next[N];
int cnt[N];
int main(){
scanf("%s",S+1); n=strlen(S+1);
int k=0;
for (int i=2;i<=n;i++){
while (k && S[k+1]!=S[i]) k=next[k];
if (S[k+1]==S[i]) k++;
next[i]=k;
cnt[k]++;
}
long long ans=0;
for (int i=n;i;i--){
ans=max(ans,(long long)i*(cnt[i]+1));
cnt[next[i]]+=cnt[i];
}
printf("%lld\n",ans);
return 0;
}