https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1277
标算Kmp啦
题目中让我们求的前缀出现次数是可以用Kmp中的nex数组推出来的
就是当前i的border长度可以累加到nex[i]的次数答案中
所以我们先求出字符串的nex数组(border),然后倒推答案(sum[nex[i]]+=sum[i])
最后输出max(sum[i]*i)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
char a[1000001];
ll nex[1000001],sum[1000001];
inline ll max(ll a,ll b){return a<b?b:a;}
int main()
{
scanf("%s",a+1);
ll l=strlen(a+1);
ll j=0;nex[1]=0;
for(ll i=2;i<=l;i++){
while(j&&a[i]!=a[j+1])j=nex[j];
if(a[i]==a[j+1])j++;
nex[i]=j;
}
for(ll i=1;i<=l;i++)if(nex[i])sum[i]=1;
for(ll i=l;i>0;i--){
ll t=nex[i];
sum[t]+=sum[i];
}
ll ans=0;
for(ll i=1;i<=l;i++)ans=max(ans,i*sum[i]);
printf("%lld",ans);
}