Period
来源:互联网 发布:三国群英传2 mac版本 编辑:程序博客网 时间:2024/05/29 19:07
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 A K,that is A concatenated K times, for some string A. Of course, we also want to know the period K.
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.
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.
3aaa12aabaabaabaab0
Test case #12 23 3Test case #22 26 29 312 4
#include <iostream>#include <cstdio>#include <cstring>const int MAX= 1e6+10;int next[MAX];char str[MAX];void getnext(int l){ int i = 0 , j = -1; next[0] = -1; while( i < l ) { if(j == -1 || str[j] == str[i]) { i++; j++; next[i] = j;}else j = next[j]; }}int main(){ int n , w = 1; while( ~scanf("%d",&n) && n ) { scanf("%s",str); int l = strlen(str); getnext(l); printf("Test case #%d\n", w++); for(int i = 0; i <= l; i++) { if(next[i] == 0) continue; if(i % (i - next[i]) == 0 && i / (i - next[i] ) > 1 ) { printf("%d %d\n",i , i / (i - next[i]));} } printf("\n"); } return 0;}
阅读全文
0 0
- Period
- Period
- period
- Period
- Period
- Period
- Period
- period
- Period
- Period
- Period
- Posting Period
- hdu1358 Period
- hdu1358 Period
- W - Period
- poj1961 Period
- HDU1358:Period
- HDU1358--Period
- 剑指offer(1):二维数组中的查找
- AsyncTask多条目加载
- 使用JasperReport和poi导出Excel
- CentOS6设置静态IP而且还可以上网
- hdu1166 敌兵布阵(线段树)
- Period
- HDU-1849-Rabbit and Grass
- 2018网易校招内推编程题 小易喜欢的数列(动态规划)
- 在ubuntu根文件系统上安装lxde桌面
- POJ
- Junit测试maven+ssm项目的过程及出现的错误总结
- block,inline和inline-block概念和区别
- UVA 11732 链式字典树
- 【JavaScript】alert()confirm() prompt()