poj 2406字符串最小环节的应用

Power Strings
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 24582 Accepted: 10329

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1

4

3

分析:本题大意就是一个字符串s最多可有几个s1连续组合而成 s = s1s1s1s1...;哈哈到这儿也就明白了这题考察什么了吧。对你没有看错是最小循环节。kmp算法的next求法是否还记得。next的一个很重要的应用就是求最小循环节。公式是这样的

假如len能整除len-next[len]则最小循环节就是len-next[len].知道最小循环节也就很easy求出最大个数了len/(len-next[len])

#include <iostream>
#include <cstring>
using namespace std;
int k = 0;
int next[1000005];
char a[1000005];
void getnext(char* a,  int len)
{
	int i = 0, j = -1;
	next[0] = -1;
	while (i < len )
	{
		while (j >= 0 && a[i] != a[j] )
		{
			j = next[j];
			k++;
		}
		i++; 
		j++;
		next[i] = j;
	}
	if (len%(len-j)==0)
	{
		cout<<len/(len-j)<<endl;
	}
	else
		cout<<"1"<<endl;
}
int main()
{

	while (cin >> a&& strcmp(a, ".")!=0)
	{
		getnext(a,  strlen(a));
	}

	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值