hdu 1358 period KMP
2015-08-11 09:42
489 查看
Period
Time Limit:1000MS Memory Limit:32768KB 64bitIO Format:%I64d & %I64u
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 thelargest 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.
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; theprefix 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
思路::若i%(i-next[i])=0,那么前i个字符为k=i/(i-next[i])个长度为i-next[i]的循环。 神奇的next数组。。。=.=
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int M = 1000010; int nxt[M]; char T[M]; int m; int ans; void getnext(){ int j,i, k; j = 0; k = -1; nxt[0] = -1; while(j<m){ if (k==-1 || T[j]==T[k]){ nxt[++j] = ++k; i=j; if(i%(i-nxt[i])==0&&nxt[i]) printf("%d %d\n",i,i/(i-nxt[i])); } else{ k = nxt[k]; } } } int main() { // freopen("in.txt","r",stdin); int cs=0; while(scanf("%d",&m)!=EOF&&m){ cs++; scanf("%s",T); printf("Test case #%d\n",cs); getnext(); printf("\n"); } return 0; }
相关文章推荐
- 租车系统
- 预加载显示图片的艺术
- Spring3.0学习笔记文档的官方网站(六)--3.4.1
- DIV+CSS高手必知的15个CSS常识
- POJ 1325 && 1274:Machine Schedule 匈牙利算法模板题
- Javascript 常见学习记录
- ad safe3.5.4.721汇编日志和笔记:
- extjs4 treepanel 多个checkbox先中 多个节点选中 多级节点展开
- Oracle 基础篇 --- 索引选项
- 有关时间
- AndroidStudio使用心得-打包发布
- 如何学习大数据技术?
- android应用程序如何调用支付宝接口
- Binary Tree Preorder Traversal
- POJ 3461 Oulipo kmp 水过
- 转发和重定向
- python垃圾回收
- 设计模式之-----职责链模式
- 17-集合框架-22-常用对象API(集合框架-TreeSet集合)
- Hadoop之YARN命令