POJ 1961 Period(KMP)
2016-08-05 22:24
274 查看
Period
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
Sample Output
题目大意:求字符串所有前缀中周期大于1的子串位置及周期数。
解题思路:求出next数组之后遍历一遍即可,周期判断方法与POJ2406相似。
代码如下:
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
题目大意:求字符串所有前缀中周期大于1的子串位置及周期数。
解题思路:求出next数组之后遍历一遍即可,周期判断方法与POJ2406相似。
代码如下:
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define EPS 1e-6 #define INF INT_MAX / 10 #define LL long long #define MOD 100000000 #define PI acos(-1.0) const int maxn = 1000005; char s[maxn]; int next[maxn]; int len; void get_next() { memset(next,-1,sizeof(next)); next[1] = 0; for(int i = 2,j = 0;i <= len;i++){ while(j > 0 && s[j + 1] != s[i]) j = next[j]; if(s[j + 1] == s[i]) j += 1; next[i] = j; } } int main() { int ncase = 0; while(scanf("%d",&len) != EOF && len){ scanf("%s",s + 1); get_next(); printf("Test case #%d\n",++ncase); for(int i = 1;i <= len;i++){ if(2 * next[i] >= i && i % (i - next[i]) == 0) printf("%d %d\n",i,i / (i - next[i])); } printf("\n"); } return 0; }
相关文章推荐
- POJ_1961_Period [ KMP ]
- poj 1961 Period [kmp, next数组的用法]
- poj 1961 Period (KMP 最短循环节)
- poj_2406Power Strings && poj_1961Period && poj_2752Seek the Name, Seek the Fame(KMP)
- poj 1961 Period(KMP)
- Kmp找字符串循环节——Period ( POJ 1961 )
- poj 1961 Period---kmp
- Poj-1961-Period-【KMP】
- POJ 1961 Period(KMP)
- poj 1961 Period(KMP)
- (POJ)1961 - Period 【KMP】
- poj_2406Power Strings && poj_1961Period && poj_2752Seek the Name, Seek the Fame(KMP)
- Period - POJ 1961 KMP
- POJ 1961-Period:KMP
- POJ 1961 Period KMP next数组的应用
- poj 1961 Period(KMP求周期)
- poj 1961 Period(未优化KMP的next数组)
- POJ 1961 Period KMP(next数组应用)
- POJ Period 1961【KMP】
- (KMP) poj POJ 1961 Period