【kmp算法】bzoj1355 [Baltic2009]Radio Transmission

引用题解:http://blog.csdn.net/wyfcyx_forever/article/details/40347425

#include<cstdio>
#include<cstring>
using namespace std;
int next[1000001],n;
char s[1000001];
void GetFail(char P[],int next[])
{
    next[0]=next[1]=0;
    for(int i=1;i<n;i++)
      {
        int j=next[i];
        while(j&&P[i]!=P[j]) j=next[j];
        if(P[i]==P[j]) next[i+1]=j+1;
        else next[i+1]=0;
      }
}
int main()
{
	scanf("%d",&n); scanf("%s",s);
	GetFail(s,next);
	printf("%d\n",n-next[n]);
	return 0;
}
posted @ 2014-12-27 15:08  AutSky_JadeK  阅读(228)  评论(0编辑  收藏  举报
TVアニメ「Charlotte(シャーロット)」公式サイト TVアニメ「Charlotte(シャーロット)」公式サイト