POJ——T 1961 Period

http://poj.org/problem?id=1961

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 18542   Accepted: 9007

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

Source

Southeastern Europe 2004

next数组的应用,代码意会吧、、

 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4
 5 using namespace std;
 6
 7 const int N(1000000+5);
 8 int len,p[N];
 9 char s[N];
10
11 inline void Get_next()
12 {
13     for(int j=0,i=2;i<=len;p[i++]=j)
14     {
15         for(;s[i]!=s[j+1]&&j>0;) j=p[j];
16         if(s[i]==s[j+1]) j++;
17     }
18 }
19
20 int main()
21 {
22     for(int i=0;scanf("%d",&len)&&len;)
23     {
24         scanf("%s",s+1);
25         memset(p,0,sizeof(p));
26         Get_next();
27         printf("Test case #%d\n",++i);
28         for(int i=1;i<=len;i++)
29         {
30             int l=i-p[i];
31             if(i!=l&&i%l==0)
32                 printf("%d %d\n",i,i/l);
33         }
34         printf("\n");
35     }
36     return 0;
37 }
时间: 2024-11-08 20:18:24

POJ——T 1961 Period的相关文章

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 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: 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求周期)

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

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 把主串的每一种前缀当作小主串,如果小主串的子串在小主串中叠加次数大于1,输出小主串长度及叠加次数。

#include<stdio.h> #define M 1000010 int i,n,next[M]; char s[M]; void getNext() { int j=-1; next[0]=-1; for(i=1;s[i];i++){ while(j!=-1&&s[j+1]!=s[i])j=next[j]; if(s[j+1]==s[i])j++; next[i]=j; } } int main() { int k=0; while(scanf("%d&quo

KMP解决字符串最小循环节相关问题

经典问题 : 给出一个由某个循环节构成的字符串,要你找出最小的循环节,例如 abababab 最小循环节当是 ab ,而类似 abab 也可以成为它的循环节,但并非最短. 分析 : 对于上述问题有两个结论 如果对于next数组中的 i, 符合 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串循环,而且 循环节长度为:    i - next[i] 循环次数为:       i / ( i - next[i] ) 水平有限,用自己的语言描述怕

常用字符串算法

简介 字符串的处理几乎无处不在,常用的字符串算法有KMP.扩展KMP.Trie树.AC自动机.Manacher.哈希.SA.SAM等. Knuth-Morris-Pratt 算法 给你两个字符串AB,询问B串是否是A串的子串(A串是否包含B串). 可以枚举从A串的什么位置起开始与B匹配,然后验证是否匹配.假如A串长度为n,B串长度为m,那么这种方法的复杂度是O (mn)的. 而KMP算法能够在线性复杂度内求出一个串在另一个串的所有匹配位置. KMP的核心思想在于构建一个前缀数组(失配数组),对于