poj 1961 Period(KMP求周期)

Period
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 15963   Accepted: 7644

Description

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A K ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the
number zero on it.

Output

For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

Sample Input

3
aaa
12
aabaabaabaab
0

Sample Output

Test case #1
2 2
3 3

Test case #2
2 2
6 2
9 3
12 4
题意:多组测试数据,第一行输入n,表示接下来要输入的字符串的长度,输入0时结束输入。

解题思路:这个题可能刚开始看的时候不知道是让求得什么,我也是看了很长时间才看懂的,只怪我太菜啊....... 其实它就是让求输入的字符串中 以任意长度下的子串,只要该子串具有整数个周期(既循环节),就输出此时子串的长度和周期数;

例如第二组测试数据:
    字符串 aabaabaabaab
    当长度为2时,子串为aa,该子串具有两个周期,每个周期为a(同循环节,每个循环节为a,一下不再叙述);
    当长度为6时,子串为aabaab,具有两个周期,每个周期为aab;
    当长度为9时,子串为aabaabaab,具有三个周期,每个周期为aab;
    当长度为12时,子串为aabaabaabaab,具有四个周期,每个周期为aab。
    结束。
    其中长度为其它的子串均不满足具有整数个周期。
具体代码:
#include <stdio.h>
#include <string.h>
char s[1000005]; 
int next[1000005];
void Make_next(int len)
{
	int i=0,j=-1;
	memset(next,0,sizeof(next));
	next[0]=-1;
	while(i<len)
	{
		if(j==-1 || s[i]==s[j])
		{
			i++; j++;
			next[i]=j;
		}
		else
			j=next[j];
	}
}
int main()
{
	int len,min_repetend,k=0;
	while(scanf("%d",&len) && len!=0)
	{
		scanf("%s",s);
		Make_next(len);
		printf("Test case #%d\n",++k);
		for(int i=1;i<=len;i++)//遍历所有的字符串长度,求满足条件的子串 
		{
			min_repetend=i-next[i];//字符串长度为 i的周期 
			if(i%min_repetend==0 && next[i]!=0)
				printf("%d %d\n",i,i/min_repetend);
		}
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(poj 1961 Period(KMP求周期))