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 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

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;
}

时间: 2024-11-03 21:07:32

poj 1961 Period(KMP求周期)的相关文章

poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14653   Accepted: 6965 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

POJ 1961 Period (KMP)

解题思路: 利用next 数组的性质求解重复子串.循环节的长度为i - next[i]; #include <iostream> #include <cstring> #include <cstdlib> #include <vector> #include <cmath> #include <algorithm> #include <cstdio> using namespace std; const int maxn

(简单) POJ 1961 Period,扩展KMP。

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 larges

POJ 1961 Period

Period Time Limit: 3000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 196164-bit integer IO format: %lld      Java class name: Main For each prefix of a given string S with N characters (each character has an ASCII code bet

poj 1961 Period 【KMP-next前缀数组的应用】

题目地址:http://poj.org/problem?id=1961 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 题目分析:给你一个字符串,最大长度1百万.输出是:以第1组样例解释,在aaa的字符串中,长度为2时,存在2个循环节a.当长度为3时,存在3个循环节a.以第二组样例解释,当长度为2时,存在2个循环节a.当长度为6时,存在2个循

poj 1348 Period(KMP)

Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10419    Accepted Submission(s): 4940 Problem Description For each prefix of a given string S with N characters (each character has an ASCI

Hdu 1358 Period (KMP 求最小循环节)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和k. 解题思路: 打表算出next数组,然后对位置i求循环节,如果满足 i % (i - Next[i]) == 0 && Next[i] != 0,所对应的循环节(i - Next[i]), 循环次数是i / (i - Next[i]) 1 #include<cstdio> 2

poj2406--Power Strings+KMP求周期

先把结论摆出来:对于长为j的字符串str[1..j],next[j]=k,则令d=j-k;如果j%d==0,则这个字符串是一个 周期串,前d个字符是其最小的循环结,共包含j/d个循环节. 现在来解决两个问题: 1)前d个字符是其循环结 下标  1   2  3  4  5  6  7  8 字符串 a   b  a  b  a  b  a  b next值 0   0  1  2  3  4  5  6 next[j]=k表示str[1...k]与str[j-k+1..j]是完全相等的;d=j-

Oulipo POJ - 3461(kmp,求重叠匹配个数)

Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter'e'. He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair