poj 1961 Period(kmp最短循环节)
2013-04-06 14:57
399 查看
题目链接
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.……………………
题意:
给定一个长度为n的字符串s,求他每个前缀的最短循环节。换句话说,对于每个i(2<=i<=n),求一个最大的整数k(如果k存在),使得s的前i个字符可以组成的前缀是某个字符串重复k次得到的。输出所有存在K的i和对应的k。
这是刘汝佳《算法竞赛入门经典训练指南》上的原题(p213),用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 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.……………………
题意:
给定一个长度为n的字符串s,求他每个前缀的最短循环节。换句话说,对于每个i(2<=i<=n),求一个最大的整数k(如果k存在),使得s的前i个字符可以组成的前缀是某个字符串重复k次得到的。输出所有存在K的i和对应的k。
这是刘汝佳《算法竞赛入门经典训练指南》上的原题(p213),用KMP构造状态转移表。
解题代码(完全参照白书):
//poj 1961 #include <stdio.h> const int maxn = 1000000 + 10; char p[maxn]; int f[maxn]; int main() { int n, t = 1; while (scanf("%d",&n) && n) { scanf("%s",p); f[0] = 0; f[1] = 0; for (int i = 1; i < n; i++) { int j = f[i]; while (p[i] != p[j] && j) j = f[j]; f[i+1] = (p[i] == p[j] ? j+1 : 0); }/*这段代码就是KMP里面核心的代码,这里没有文本串, 我们只需要处理模板即可*/ printf("Test case #%d\n",t++); for (int i = 2; i <= n; i++) { if (f[i] > 0 && i % (i-f[i]) == 0) printf("%d %d\n",i, i / (i-f[i])); } puts(""); } return 0; }
相关文章推荐
- LA 3026 && POJ 1961 Period(KMP求前缀的最短循环节)
- poj 1961 Period(KMP, 最短循环节)
- poj 1961 Period (KMP 最短循环节)
- poj 1961 Period(kmp最短循环节)
- LA 3026 && POJ 1961 Period (利用kmp中的next数组找最小的循环节 )
- Kmp找字符串循环节——Period ( POJ 1961 )
- KMP 求最小循环节 POJ - 1961 Period
- poj1961--Period(KMP求最小循环节)
- POJ 1961 Period(KMP最小循环节)
- POJ 1961 Period【KMP最小循环节】
- POJ 1961(KMP, 最短循环节)
- poj 1961 Period (KMP+最小循环节)
- POJ 1961 Period KMP 最小循环节
- POJ 1961Period / POJ 2406 Power Strings / POJ 3461 Oulipo /SDUT 2747 循环节 初涉KMP
- 【 poj 1961 】Period 【KMP 求所有前缀的循环节】
- POJ 1961 Period——kmp求最小循环节
- Period POJ - 1961(kmp求最大周期,最小循环节)
- POJ 1961 Period(kmp循环节)
- POJ 1961 Period (KMP)
- POJ 1961 Period(KMP)