题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358

分析:已知字符串,求其由最小循环节构成的前缀字符串。

/*Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3507 Accepted Submission(s): 1766 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
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
*/
#include <cstdio>
#include <cstring>
const int maxn = + ;
char a[maxn];
int next[maxn], n;
void getNext()
{
int k = -, j = ;
next[] = -;
while(j < n){//遍历字符串
if(k == - || a[j] == a[k]) next[++j] = ++k;
else k = next[k];
}
} int main()
{
int C = ;
while(~scanf("%d", &n) && n){
scanf("%s", a);
getNext();
printf("Test case #%d\n", ++C);
for(int i = ; i <= n; i++){//遍历所有循环节长度
int len = i;
int cnt = len-next[len];
if(cnt == ){//循环节长度为1,直接输出
printf("%d %d\n", len, len);
}
else if(cnt != len && (len % cnt) == ){
printf("%d %d\n", len, len/cnt);
}
}
printf("\n");
}
return ;
}

hdu 1358 Period 最小循环节的更多相关文章

  1. HDU 6740 kmp最小循环节

    题意:给一个无线循环小数的前几位,给n,m 选择其中一种出现在前几位的循环节方式(a个数),循环节的长度b 使得n*a-m*b最大 样例: 2 1 12.1212 输出 6 选择2,2*1-1*1=1 ...

  2. HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  3. hdu 3746 Cyclic Nacklace(kmp最小循环节)

    Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...

  4. poj1961 Period kmp解决找字符串的最小循环节

    /** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个 ...

  5. HDU1358 Period —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  6. hdu 3746 Cyclic Nacklace (KMP求最小循环节)

    //len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...

  7. hdu 3746 Cyclic Nacklace(next数组求最小循环节)

    题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...

  8. HDU - 4333 Revolving Digits(拓展kmp+最小循环节)

    1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> ...

  9. HDU 1358 Period(KMP计算周期)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...

随机推荐

  1. stl源码剖析 详细学习笔记deque(2)

    //---------------------------15/3/13---------------------------- self&operator++() { ++cur; if(c ...

  2. rabbitMQ教程(三)一篇文章看懂rabbitMQ

    一.rabbitMQ是什么: RabbitMQ,遵循AMQP协议,由内在高并发的erlanng语言开发,用在实时的对可靠性要求比较高的消息传递上. 学过websocket的来理解rabbitMQ应该是 ...

  3. redis在php中常用的语法(转)

    String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象 $redis-> ...

  4. 金蝶盘点机PDA条码数据采集器WMS系统具体有哪些功能

    1.  使用汉码盘点机PDA实现仓库条码管理的好处 (1)  传统电脑管理软件出入库需要来回电脑跑人工手工电脑录单效率低,通过人眼识别商品品种和清点商品数量,容易造成录单错误.从而造成电脑管理软件库存 ...

  5. Fabric Dev开发调试模式的搭建过程

    在利用Fabric开发Chaincode的时候,调试Chaincode显得尤为不方便,因为Chaincode正常应该运行在Docker容器中,每次修改Chaincode后想要使其更改生效必须得对Cha ...

  6. Python中元组,列表,字典的区别

    http://blog.csdn.net/yasi_xi/article/details/38384047

  7. YQCB冲刺第二周第四天

    站立会议 任务看板 今天的任务为实现精准查账的功能. 昨天的任务为实现查看消费明细的功能. 遇到的问题为忘记在记账记录的表中添加用户名一栏,这样导致不同用户登录时查看消费明细会显示所有用户的所有记录.

  8. Docker打DB2 9.7镜像采坑相关

    概况:以centos:7.2.1511镜像为基础镜像,使用docker commit方式进行构建   步骤: 运行centos7.2.1511镜像(以特权模式运行,后续内核参数修改必需参数) dock ...

  9. 关于github的使用心得

    https://github.com/JavaLizheng/test git常用命令: git config :配置git git add:更新working directory中的文件至stagi ...

  10. RYU 灭龙战 second day(内容大部分引自网络)

    RYU 灭龙战 second day(内容大部分引自网络) 写好的markdown重启忘了保存...再写一次RLG 巨龙的稀有装备-RYU代码结构 RYU控制器代码结构的总结 RYU入门教程 RYU基 ...