POJ 1961 Period (KMP)
2016-08-17 23:58
351 查看
Period
Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
POJ 1961
Appoint description:
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
这道题诶,以前做与这道类似的题的时候不是这么简单,谁知道还有这么简单的步骤,具体解释在代码上,复制一下别人精辟的话,KMP中的get_next(),或者get_nextval(),对next数组的应用。next[len]是最后一个字符跳的步长,如果他有相同字符串,则该串长度是len-next[len],如果整个长度len能分解成x个这种串(能整除),就得到ans了。否则不能分解。只能是由他自己组成串,长度为1。
Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
POJ 1961
Appoint description:
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
这道题诶,以前做与这道类似的题的时候不是这么简单,谁知道还有这么简单的步骤,具体解释在代码上,复制一下别人精辟的话,KMP中的get_next(),或者get_nextval(),对next数组的应用。next[len]是最后一个字符跳的步长,如果他有相同字符串,则该串长度是len-next[len],如果整个长度len能分解成x个这种串(能整除),就得到ans了。否则不能分解。只能是由他自己组成串,长度为1。
#include <cstdio> #include <cstring> #define M 1000010 int n, next[M]; char str[M]; void getnext() { int k = -1, i = 0; next[0] = -1; while(i < n) { if(k == -1 || str[i] == str[k]) { i++, k++; next[i] = k; } else { k = next[k]; } } } int main() { int ca = 1; while(scanf("%d", &n) && n) { scanf("%s", str); getnext(); printf("Test case #%d\n", ca++); for(int i=2; i<=n; i++) { int len = i - next[i];//记录一下相差 if(i != len && i % len == 0)//取余成功就表示可以循环 { printf("%d %d\n", i, i / len); } } printf("\n"); } return 0; }
相关文章推荐
- poj 1961 Period (KMP)
- |poj 1961|KMP|Period
- poj 1961 Period(数据结构:KMP)
- POJ 1961 Period(KMP)
- poj 1961 Period(KMP)
- poj 1961 Period(kmp最短循环节)
- POJ 1961 Period(KMP 求循环)
- POJ 1961 Period——kmp求最小循环节
- poj 1961 Period kmp基础
- POJ 1961 Period(KMP next数组巧用)
- poj 1961 Period(kmp最短循环节)
- 【 poj 1961 】Period 【KMP 求所有前缀的循环节】
- [poj 1961]Period[kmp]
- poj 1961 Period (KMP)
- POJ 题目1961 Period(KMP水)
- KMP POJ 1961 Period 解题报告
- poj 1961 Period 【KMP-next前缀数组的应用】
- POJ 1961 Period【KMP最小循环节】
- POJ-1961(Period)(kmp())
- POJ_1961 Period(KMP)