码迷,mamicode.com
首页 > 其他好文 > 详细

【HDU 3746】Simpsons’ Hidden Talents(KMP求循环节)

时间:2016-07-31 20:39:52      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

求next数组,(一般有两种,求循环节用的见代码)求出循环节的长度。

#include <cstdio>
#define N 100005
int n,next[N];
char s[N];
int main(){
	scanf("%d",&n);
	while(n--){
		scanf("%s",s);
		int i=0,k=-1;
		next[0]=k;
		while(s[i]){
			if(k==-1||s[i]==s[k])
			{
				i++;
				k++;
				next[i]=k;
			}else
				k=next[k];
		}
		int b=i-next[i],ans=0;
		if(b==i||i%b)ans=b-i%b;//本身是循环节或者不能完全循环。
		printf("%d\n",ans);
	}
}

  

【HDU 3746】Simpsons’ Hidden Talents(KMP求循环节)

标签:

原文地址:http://www.cnblogs.com/flipped/p/5723815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!