https://jzoj.net/senior/#main/show/4876
想法:
拓展KMP的next数组的和
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#define maxn 1000010
#define ll long long
using namespace std;
char s[maxn];
ll next[maxn],i,j,p,a,n,ans;
int main()
{
// freopen("gene.in","r",stdin);
// freopen("gene.out","w",stdout);
scanf("%s",s+1);
n=strlen(s+1);
p=0;
j=-1;
next[0]=n;
a=0;
for (i=1;i<n;i++)
{
if ((j<0)||(i+next[i-a]>=p))
{
if (j<0)
{
j=0;
p=i;
}
while ((j<n)&&(p<n)&&(s[j+1]==s[p+1]))
{
j++;
p++;
}
next[i]=j;
a=i;
}
else next[i]=next[i-a];
j--;
}
for (i=0;i<n;i++)
ans+=next[i];
printf("%lld\n",ans);
}