hdu-1358 Period 【kmp】

Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3443    Accepted Submission(s): 1727

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

题意: 从字符串第二个位置开始,前面的字符是否是循环的字符串,如果是,输出当前位置及其循环的个数。

又是kmp的getnext()函数循环节的考察

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<queue>
#include<stack>
#include<set>
#include<map>

using namespace std;

char  b[1000500];
int Next[1000500];

void get_next(char b[], int m)
{
	int i = 0,j = -1;
	memset(Next,0,sizeof(Next));
	Next[0] = -1;
	while (b[i])
	{
		if (j == -1 || b[i] == b[j])
		{
			++i;
			++j;
			Next[i] = j;
		}
		else
			j = Next[j];
	}
}

int main()
{
	int cases = 1, n, m, i, j;

	while (scanf("%d",&n)!=EOF && n)
	{
		scanf("%s",b);
		get_next(b, n);
		printf("Test case #%d\n",cases++);

		for (int i = 2; b[i-1]; i++)
		{
			int t = Next[i];
			int s = i - t;
			if (i % s == 0 && i/s>1)
			{
				printf("%d %d\n",i,i/s);
			}
		}
		printf("\n");
	}
	return 0;
}
时间: 2024-11-07 21:53:02

hdu-1358 Period 【kmp】的相关文章

poj1961 &amp; hdu 1358 Period(KMP)

poj 题目链接:http://poj.org/problem?id=1961 hdu题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 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

HDU1358 Period【KMP】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意: 给你长度为N的字符串s,求字符串s的循环前缀的长度和循环的次数. 例如:长度为8的字符串:"abababab" 长度为4的前缀"abab",循环前缀为"ab",循环2次 长度为6的前缀"ababab",循环前缀为"ab",循环3次 长度为8的前缀"abababab",循环

HDU 1358 Period(kmp简单解决)

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

HDU - 1358 - Period (KMP)

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

hdu 1686 Oulipo【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

HDU 1686:Oulipo 【KMP】

Oulipo Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 45   Accepted Submission(s) : 29 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without

HDU 1358 Period(kmp)

next数组的应用 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #define L(x) (x<<1) #define R(x) (x<<1|1) #def

【动态规划】【KMP】HDU 5763 Another Meaning

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007). 题目思路: [动态规划][KMP] 题目有点绕,看看样例就懂了.其实不用KMP直接用substr就能做. 首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 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 = "