题解 P3435 【[POI2006]OKR-Periods of Words】

luogu

#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;
} 
posted @ 2019-05-24 16:27  G_A_TS  阅读(468)  评论(0编辑  收藏  举报