题目链接:https://vjudge.net/problem/HDU-1358

Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9652    Accepted Submission(s): 4633

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

 
Recommend
JGShining

题解:

给出一个字符串s,问字符串s的哪些前缀是循环的(以某个字符串至少循环两次)。

此题需要熟悉kmp的next数组。

1.在get_next函数中,如果当前匹配到前缀i:可知前缀i的最小循环节为i-next[i]。

2.如果循环节不等于前缀i自身,且前缀i的长度能被循环节的长度整除,则符合题目要求,输出答案。

3.详情还是请看代码吧。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; char x[MAXN];
int Next[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
int r = i-Next[i]; //当前前缀的最小循环节
if(i!=r && i%r==) //最小循环节不等于自己,且除得尽
printf("%d %d\n", i, i/(i-Next[i]));
}
} int main()
{
int len, kase = ;
while(scanf("%d", &len) && len)
{
scanf("%s", x);
printf("Test case #%d\n", ++kase);
get_next(x, len);
printf("\n");
}
}

HDU1358 Period —— KMP 最小循环节的更多相关文章

  1. hdu1358 Period kmp求循环节

    链接 http://acm.hdu.edu.cn/showproblem.php?pid=1358 思路 当初shenben学长暑假讲过,当初太笨了,noip前几天才理解过来.. 我也没啥好说的 代码 ...

  2. Cyclic Nacklace hdu3746 kmp 最小循环节

    题意:给出一段字符串  求最少在最右边补上多少个字符使得形成循环串(单个字符不是循环串) 自己乱搞居然搞出来了... 想法是:  如果nex[len]为0  那么答案显然是补len 否则  答案为循环 ...

  3. Common Divisors CodeForces - 182D || kmp最小循环节

    Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...

  4. UVAlive 3026 KMP 最小循环节

    KMP算法: 一:next数组:next[i]就是前面长度为i的字符串前缀和后缀相等的最大长度,也即索引为i的字符失配时的前缀函数. 二:KMP模板 /* pku3461(Oulipo), hdu17 ...

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

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

  6. 【KMP+最小循环节】F. Cyclic Nacklace

    https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/F [题意] 给定一个字符串,问在字符串后最少添加多少个字母,得到的新字符串能是前 ...

  7. [poj 2185] Milking Grid 解题报告(KMP+最小循环节)

    题目链接:http://poj.org/problem?id=2185 题目: Description Every morning when they are milked, the Farmer J ...

  8. hdu3746(kmp最小循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 题意:问在一个字符串末尾加上多少个字符能使得这的字符串首尾相连后能够循环 题解:就是利用next ...

  9. HDU 6740 kmp最小循环节

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

随机推荐

  1. http 400报错

    http 400报错---springmvc相关: 1.使用了json入参,传递给了对象,如果对象里的属性,如这里的Bonus是int类型,你传入了非int类型,这里就会报400 2.使用了@Requ ...

  2. idea16使用maven命令clean、编译、打包jar或者war

    项目环境:idea16+jdk1.7+maven-3.3.9 项目说明:编写简单的java类,使用maven命令生成jar包,然后执行------->"java  -classpath ...

  3. zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)

    3799567 2014-10-14 10:13:59                                                                     Acce ...

  4. Android 获取屏幕事件的坐标

    通常情况下我们只能获取当前Activity的画面坐标,那有时候我们需要做到一种类似于c++ hook的后台运行程序能够监听到前台用户的操作并记录下来,往往这类程序都是为自动化测试服务的. Androi ...

  5. python(3)- 常用快捷键及基础命令

  6. STM32 GPIO寄存器 IDR ODR BSRR BRR

    IDR是查看引脚电平状态用的寄存器,ODR是引脚电平输出的寄存器 下面内容的原文:http://m646208823.blog.163.com/blog/static/1669029532012931 ...

  7. Codeforces 12 D Ball

    Discription N ladies attend the ball in the King's palace. Every lady can be described with three va ...

  8. 老系统优化同步导入10w+Excel数据 秒级

    背景:老系统asp.net 2.0项目使用客户反应,某个业务每个月导入数据操作很慢,大致需要15-30分钟才会导入完毕: 分析:导入慢的原因:  .数据量过大,且采用的是同步,单个excel shee ...

  9. iinflux数据库使用

    特殊用法: http://blog.fatedier.com/2016/07/05/research-of-time-series-database-influxdb/ 创建表及表中的key和valu ...

  10. windows redis 服务安装坑

    环境 winserver 2012 最新版的redis:3.0.503 redis-server.exe   --service-install   redis.windows.conf    --m ...