poj 1961 Period(KMP求周期)

来源:互联网 发布:2015年十大网络流行语 编辑:程序博客网 时间:2024/05/17 23:43

Period
Time Limit: 3000MS Memory Limit: 30000KTotal 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 AK ,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

3aaa12aabaabaabaab0

Sample Output

Test case #12 23 3Test case #22 26 29 312 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;}elsej=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;}


0 0
原创粉丝点击