poj_1961 Period(KMP)
来源:互联网 发布:张艺谋奥运会知乎 编辑:程序博客网 时间:2024/05/01 23:37
Period
Time Limit: 3000MS Memory Limit: 30000KTotal Submissions: 17129 Accepted: 8249
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,i]的循环次数大于1的最短循环节(如果存在),输出i和循环次数K
把字符串P="aabaabaabaab"每个字符从0排到n-1,求出失配函数f[i]:
i 0 1 2 3 4 5 6 7 8 9 10 11 12P[i] a a b a a b a a b a a b 无f[i] 0 0 1 0 1 2 3 4 5 6 7 8 9
失配函数f[i]也就是匹配第i号字符失败时应转移到第几号字符重新开始匹配。
可以发现如果字符串[0,i-1]有循环节,那么i-f[i]刚好是循环节的长度,这样一来循环次数也可知了。
注意循环次数要大于1。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;int n;char P[maxn];int f[maxn];void getFail(){ int m = strlen(P); f[0] = f[1] = 0; //递推边界初值 for(int i = 1; i < m; i++) { int j = f[i]; while(j && P[i] != P[j]) j = f[j]; f[i+1] = P[i]==P[j] ? j+1 : 0; }}int main(){ int casen = 0; while(~scanf("%d", &n) && n) { scanf("%s", P); getFail(); printf("Test case #%d\n", ++casen); for(int i = 1; i <= n; i++) if(f[i] > 0 && i%(i-f[i]) == 0) printf("%d %d\n", i, i/(i-f[i])); printf("\n"); } return 0;}
0 0
- poj_1961 Period(KMP)
- POJ_1961 Period(KMP)
- poj_1961 Period
- Period(KMP算法)
- [POJ1961]Period(KMP)
- HDU1358 Period(KMP)
- HDU1358-Period(KMP)
- HDU1358 Period(KMP)
- Period(KMP算法)
- LA - 3026 - Period(KMP)
- hdu 1358 Period(KMP)
- hdu 1358 Period(KMP)
- HDU 1358 Period(KMP)
- POJ 1961-Period (KMP)
- POJ 1961 Period(KMP)
- uvalive3026 Period (KMP+结论)
- HDU 1358 Period(kmp)
- UVALive - 3026 - Period (KMP)
- BZOJ1651--[Usaco2006 Feb]Stall Reservations 专用牛棚
- 开发过程遇到的一些问题(三)
- 算法提高 数字黑洞
- opencl _高斯核卷积
- 安卓OkHttp封装
- poj_1961 Period(KMP)
- 自定义控件:流式布局
- Spring Boot 入门
- ISO8583报文中的编、解码总结
- 【数据结构】-线性表-链表 熟练度max=3(不建立新节点,使链表L倒序)
- android7.1 保存图片到系统图库
- windows环境下编译skia
- 多线程情况下如何捕获线程中的异常?
- 抽象类(abstract)和接口(interface)的区别