POJ-2406-kmp求最小循环节

http://poj.org/problem?id=2406

求一个字符串最多由多少个子串重复得到,用kmp里求next数组的方法求得最后一个字母的next【】

如果 next[n]指向的位置的字符与自身相等,且 len%(n-next[n])==0 ,则可证明 字符串存在  len/(n-next[n])个循环节,不满足任一条件,循环节长度只能为1


证明见 :http://blog.csdn.net/viphong/article/details/48498595


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
#define  inf 0x7fffffff
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 1000005;
using namespace std;

int nextval[maxn];
void get_next(char *t,int len)	//失配函数

{
	int i,j;
	i=1; 
	nextval[1]=0;
	j=0;
	while(i<len)
	{
		
		if (j==0||t[i]==t[j])
		{ 
			j++;
			i++;  
		//	if (t[i]!=t[j])
			nextval[i]=j;
		//	else
		//		nextval[i]=nextval[j];
			
			
		}
		else
			j=nextval[j];
	}
	
}
 
char tm[maxn];  //主串
char nm[maxn];

int judge(int x)
{
return 0;
}
int main( )
{    
 
	while(scanf("%s",tm+1)!=EOF)
	{
		if (strcmp(tm+1,".")==0) break;
		int len=strlen(tm+1);
		get_next(tm,len);
		int maxx=nextval[len];
		int ans;
		if (len%(len-maxx)==0&& tm[len]==tm[nextval[len]]) 
			ans=len/(len-maxx);
		else
			ans=1;
 
			printf("%d\n",ans);
	
	
	}
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值