poj 1961 Period
来源:互联网 发布:欧姆龙传感器淘宝 编辑:程序博客网 时间:2024/06/18 15:55
点击打开链接
Period
Time Limit: 3000MS Memory Limit: 30000KTotal Submissions: 18865 Accepted: 9170
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
题目大意:
给你一个字符串,求这个字符串到第i个字符为止的循环节的次数。
比如aabaabaabaab,长度为12.到第二个a时,a出现2次,输出2.到第二个b时,aab出现了2次,输出2.到第三个b时,aab出现3次,输出3.到第四个b时,aab出现4次,输出4.
是poj 2406的加强版,此链接是2406
#include <iostream>#include <cstdio>#include <cstring>const int N=1000000;int Next[N+5];char str[N+5];int len;using namespace std;void getNext()//next数组,跳转表{Next[0]=-1;int i=0,j=-1;while(i<len){if(j==-1||str[i]==str[j]){i++;j++;Next[i]=j;}elsej=Next[j];}}int main(){int k=1;while(scanf("%d",&len),len){scanf("%s",str);getNext();printf("Test case #%d\n",k++);for (int i=1;i<=len;i++){if(i%(i-Next[i])==0&&i!=i-Next[i])//循环节printf("%d %d\n",i,i/(i-Next[i]));}printf("\n");}return 0;}
参考网址
http://blog.csdn.net/niushuai666/article/details/6967716
阅读全文
0 0
- POJ 1961 Period
- poj 1961 Period
- POJ 1961 Period
- poj 1961 Period
- poj 1961:Period (KMP)
- POJ-1961 Period
- poj 1961 Period
- POJ 1961 - Period
- poj 1961 Period
- poj 1961Period
- poj 1961 Period (KMP)
- POJ-1961-Period
- POJ 1961 Period
- poj 1961 Period
- poj 1961 Period
- poj 1961 Period---kmp
- POJ 1961 Period
- poj-Period-1961
- JVM系列知识汇总
- 欢迎使用CSDN-markdown编辑器
- ue4 碰撞检测测试
- vue-resource上传文件(POST multipart/form-data到koa-multer)
- Java面向对象(2)
- poj 1961 Period
- python实现单例模式
- 如何转载别人的博客
- S5PV210开发 -- 通信
- php微信公众号开发,入门篇(实现了关注公众号发送欢迎信息,发关键词自回复)
- 最小生成树-prim算法
- 【java基础:IO】Java的IO知识整理!
- azkaban 编译使用方法
- Maven建立父子关系项目工程,建立依赖关系结构