题解 P3435 【[POI2006]OKR-Periods of Words】
#include<bits/stdc++.h>
using namespace std;
long long n,next[1000010],ans;
char c[1000010];
int main()
{
scanf("%d%s",&n,c+1);
for(int i=2;i<=n;i++)
{
int j=next[i-1];
if(j>0&&c[i]!=c[j+1])
{
j=next[j];
}
if(c[i]==c[j+1])
{
next[i]=j+1;
}
else
{
next[i]=0;
}
}
for(int i=1;i<=n;i++)
{
int j=i;
while(next[j])
{
j=next[j];
}
if(next[i])
{
next[i]=j;
}
ans+=i-j;
}
cout<<ans;
}

浙公网安备 33010602011771号