题目链接: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. sql语句——根据身份证号提取省份、出生日期、年龄、性别。

    原表 sql语句: SELECT ) ' then '北京市' ' then '天津市' ' then '河北省' ' then '山西省' ' then '内蒙古自治区' ' then '辽宁省' ...

  2. 【第九课】MriaDB密码重置和慢查询日志

    目录 1.如何进行修改MariaDB的密码 2.Mariadb的慢查询日志 1.如何进行修改MariaDB的密码 记得root密码的修改方式: [root@localhost ~]# mysqladm ...

  3. SQL Server 常用内置函数

    本文用于收集在运维中经常使用的系统内置(built-in)函数,持续整理中 一,常用Metadata函数 1,查看数据库的ID和Name db_id(‘DB Name’),db_name('DB ID ...

  4. 杂谈---小故事小道理,面试中的小技巧(NO.2)

    本篇是接着上一篇面试随笔的,上一次有猿友反应写的有些“扯淡”,LZ思来想去最大的原因可能是由于上一章写的全是一些大忌,既然是大忌,那么在现实当中发生的概率还是相对较小的,大部分人还是很少在面试中犯如此 ...

  5. 接口自动化测试框架-AIM

    最近在做公司项目的自动化接口测试,在现有几个小框架的基础上,反复研究和实践,搭建了新的测试框架.利用业余时间,把框架总结了下来. AIM框架介绍 AIM,是Automatic Interface Mo ...

  6. 转 ssh-keygen 的 详解

    为了让两个linux机器之间使用ssh不需要用户名和密码.所以采用了数字签名RSA或者DSA来完成这个操作. 模型分析 假设 A (192.168.20.59)为客户机器,B(192.168.20.6 ...

  7. 阿里(蚂蚁,菜鸟)+百度+网易面经(JAVA)

    本人综述: 本人本科和硕士均是毕业于普通的双非院校,参加过ACM竞赛,阿里面了三次全被拒了,网易收到offer. 阿里面试: 阿里的面试问的都是很基础的东西,如操作系统的简单问题,计算机网络的基本问题 ...

  8. 【Android】Scroller分析

    mScroller.getCurrX() //获取mScroller当前水平滚动的位置 mScroller.getCurrY() //获取mScroller当前竖直滚动的位置 mScroller.ge ...

  9. svn命令行创建和删除分支和tags

    首页 分类首页   目录 原文: http://blog.csdn.net/yangzhongxuan/article/details/7519948 http://zccst.iteye.com/b ...

  10. JavaScript高级程序设计学习笔记2

    垃圾收集原理: 找出不再使用的变量,然后释放其内存. js中最常用的垃圾收集方法是标记清除,当变量进入环境时,就将变量标记为“进入环境”,当变量离开环境时,将其标记为“离开环境”,最后由垃圾收集器完成 ...