bzoj 1355: [Baltic2009]Radio Transmission【kmp】

kmp复健,答案是n-next[n]

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1000005;
int n,ne[N];
char s[N];
int main()
{
	scanf("%d%s",&n,s+1);
	int j=0;
	for(int i=2;i<=n;i++)
	{
		while(s[j+1]!=s[i]&&j)
			j=ne[j];
		if(s[j+1]==s[i])
			j++;
		ne[i]=j;
	}
	printf("%d\n",n-ne[n]);
	return 0;
}
posted @ 2018-09-07 22:03  lokiii  阅读(143)  评论(0编辑  收藏  举报