Period - POJ 1961 KMP
2014-05-22 18:57
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
题意:输出所有的前缀循环情况。
思路:KMP。
AC代码如下:
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 12498 | Accepted: 5828 |
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
题意:输出所有的前缀循环情况。
思路:KMP。
AC代码如下:
#include<cstdio> #include<cstring> using namespace std; char str[1000010]; int f[1000010]; int n; void getfail() { int i,j; memset(f,0,sizeof(f)); f[0]=0;f[1]=0; for(i=1;i<=n;i++) { j=f[i]; while(j && str[i]!=str[j]) j=f[j]; if(str[i]==str[j]) f[i+1]=j+1; else f[i+1]=0; } } int main() { int t=0,i,j,k; while(~scanf("%d",&n) && n>0) { scanf("%s",str); getfail(); if(t!=0) printf("\n"); printf("Test case #%d\n",++t); for(i=2;i<=n;i++) if(f[i] && i%(i-f[i])==0) printf("%d %d\n",i,i/(i-f[i])); } }
相关文章推荐
- POJ_1961_Period_KMP
- poj 1961 Period(KMP训练指南例题)
- POJ 1961 Period --------KMP
- POJ 1961 Period(KMP最小循环节)
- POJ 1961 Period (KMP)
- POJ 1961 && HDU 1358 Period(kmp)
- Period POJ - 1961(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 1961Period / POJ 2406 Power Strings / POJ 3461 Oulipo /SDUT 2747 循环节 初涉KMP
- KMP 求最小循环节 POJ - 1961 Period
- POJ-1961 Period-KMP前缀串重复次数
- POJ - 1961 Period解题报告(kmp next数组巧用)
- poj 1961 Period (kmp next 数组的应用)
- POJ - 1961 Period(KMP)
- POJ 1961 Period(KMP求一个串的重复子串)