POJ 1961 Period(KMP)
来源:互联网 发布:cookie登录 php 编辑:程序博客网 时间:2024/05/29 16:55
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.
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
3aaa12aabaabaabaab0
Sample Output
Test case #12 23 3Test case #22 26 29 312 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;}
0 0
- 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 1961Period (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)
- Period - POJ 1961 KMP
- POJ 1961 Period (KMP)
- POJ Period 1961【KMP】
- 60. Permutation Sequence
- CODEFORCES 272D Dima and Two Sequences <排列组合>
- Cocos网络访问——代理模式(2)
- Hibernate应用的优化
- 二叉树
- POJ 1961 Period(KMP)
- 【CQYZ-vijos】P1333 舞伴的搭配(贪心算法)
- C++中虚函数解析
- 日期类问题
- 分享一个圆角自定义的漂亮AlertDialog
- java旅行第二站--JavaSE第四天
- 【字符串8】正则表达式匹配
- codeforces #365 div2ABC题解
- Android 6.0+ 运行时权限