HDOJ 1358 Period
来源:互联网 发布:青少年违法犯罪数据 编辑:程序博客网 时间:2024/05/21 19:56
Period
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2258 Accepted Submission(s): 1117
Problem 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 file 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
3aaa12aabaabaabaab0
Sample Output
Test case #12 23 3Test case #22 26 29 312 4
i - fail [ i ] 正好是一个循环节的长度
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=1001000;char str[maxn];int N,fail[maxn];void getfail(){ int n=strlen(str); fail[0]=fail[1]=0; for(int i=1;i<n;i++) { int j=fail[i]; while(j&&str[i]!=str[j]) j=fail[j]; fail[i+1]=(str[i]==str[j])?(j+1):0; }}int main(){ int cas=1; while(scanf("%d",&N)!=EOF&&N) { scanf("%s",str); printf("Test case #%d\n",cas++); getfail(); for(int i=2;i<=N;i++) { if(fail[i]&&(i%(i-fail[i])==0)) { printf("%d %d\n",i,i/(i-fail[i])); } } putchar(10); } return 0;}
- HDOJ 1358 Period
- 【KMP】 HDOJ 1358 Period
- HDOJ Period 1358【KMP】
- HDOJ 1358 Period
- HDOJ 1358 Period
- HDOJ 1358 Period
- HDOJ 题目1358 Period(kmp)
- hdoj-【5908 Abelian Period】
- 【HDOJ】5908 Abelian Period
- HDOJ 1358 Period(KMP next数组运用)
- 1358 Period
- POJ 1961 Period || HDU 1358 Period || ZOJ 2177 Period
- HDU 1358 Period (KMP)
- HDU 1358 Period
- hdu 1358 Period
- hdu 1358 Period
- HUD 1358 Period
- Period 1358 KMP
- Solr使用初探——Solr的安装环境与配置
- 在菜单中显示图标
- 南阳理工OJ_日期计算
- ubuntu 下软件安装卸载与查看
- 关于strlen函数的一个问题
- HDOJ 1358 Period
- 直接插入排序
- linux设备驱动归纳总结(二):模块的相关基础概念
- 大型网站架构演变过程、大并发服务器架构
- 心情
- 2013年考场改革后第四次学车
- 在布署Tomcat时,出现The scratchDir you specified is unusable
- Mysql中文乱码问题完美解决方案
- 2复选框和单选按钮