题目在这儿

Period

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

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

题意:给出一个字符串,然你找到所有的前缀是循环串的前缀长度,和对应的循环节的循环次数
题解:

总结一下,如果对于next数组中的 i, 符合 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串0-n-1循环,而且

循环节长度为:   i - next[i]

循环次数为:       i / ( i - next[i] )

如果要问为什么可以自习用几个栗子,然后啃一啃

或者看这里:

next[i]数组的含义是0-i-1个字符中前next[i]和倒数next[i]是完全相同的,那么如果next[i] < (i-1)/2 (即如果这完全相同的前缀和后缀不重合的话)那么有i-next[i]>(i-1)/2则i不可能有i%[i-next[i]]==0这种情况是不可能有循环串的,因为如果是循环串,公共的前缀和后缀肯定是重合的

下面考虑如果重合的情况,如果重合的话,那么一定有i-next[i]就是最小的循环节长度,自己画个图仔细理解一下
然后就有了上面的结论。还是要理解好next的本质才可以
 
下面给出这个题的代码:
 /*
对next的理解更深入了点儿。
字符编号从0开始,那么if(i%(i-next[i])==0),注意,还要保证循环次数大于1,则i前面的
串为一个轮回串,其中轮回子串出现i/(i-next[i])次。
还要注意Next数组处理的是当前这个数的前缀,所以要处理最后一个字符就要再有面再加一位防止漏解
Print a blank line after each test case.注意这个pe了一次
*/ #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = ;
char b[N];
int Next[N];
void get_next(int n)
{
for(int i = ; i <= n; i++) Next[i]=;
int tm;
tm = Next[] = -;
int j = ;
while(j<n){
if(tm<||b[j]==b[tm]) {
Next[++j] = ++tm;
}
else tm = Next[tm];
}
return;
}
int main()
{
int c = ;
int n;
while(~scanf("%d",&n),n)
{
getchar();
scanf("%s",b);
b[n] = '#';
get_next(n);
printf("Test case #%d\n",c++);
for(int i = ; i <= n; i++){
int t = i - Next[i];
if(i%t==&&i/t>){
printf("%d %d\n",i,i/(i-Next[i]));
}
}
printf("\n");
}
return ;
}

hdu_1358Period(kmp找循环前缀)的更多相关文章

  1. POJ:2185-Milking Grid(KMP找矩阵循环节)

    Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...

  2. HDU 2802 F(N)(简单题,找循环解)

    题目链接 F(N) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  3. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  4. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

  5. (收藏)KMP算法的前缀next数组最通俗的解释

    我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度. 当然我们可以看到这个算法针对的是子串有对称属性, ...

  6. POJ 2752 Seek the Name, Seek the Fame kmp(后缀与前缀)

    题意: 给你一个串T,找出串T的子串,该串既是T的前缀也是T的后缀.从小到大输出所有符合要求的串的长度. 分析: 首先要知道KMP的next[i]数组求得的数值就是串T中的[1,i-1]的后缀与串T中 ...

  7. HDU 3746 Cyclic Nacklace (KMP找循环节)

    题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...

  8. bzoj 4974 [Lydsy1708月赛]字符串大师 KMP 最小循环元 构造

    LINK:字符串大师 给出一个字符串的每个前缀的最小循环元 还原字典序最小的原字符串. 一个比较显然的结论 或者说 学过KMP的都知道 对于每个前缀i求出nex数组后 那么i-nex[i]为最小循环元 ...

  9. HDOJ-3746(KMP+最小循环结)

    Cyclic Nacklace HDOJ-3746 本题还是使用KMP算法,需要使用到前缀数组 利用前缀数组计算最小循环节:即t=n-pi[n-1]. 最后输出还需要的珠子,当然还有判断什么时候输出为 ...

随机推荐

  1. iOS 如何优化 App 的启动时间

    App 运行理论 main() 执行前发生的事 Mach-O 格式 虚拟内存基础 Mach-O 二进制的加载 理论速成 Mach-O 术语 Mach-O 是针对不同运行时可执行文件的文件类型. 文件类 ...

  2. tesserat训练中文备忘录

    最近用OCR识别身份证,用的tesseract引擎.但是google自带的中文库是在太慢了,尤其是对于性别.民族这样结果可以穷举的特征信息而言,完全可以自己训练字库.自己训练字库不仅可以提高识别速度, ...

  3. openstack操作之二 restful api

    Restful api 是openstack各服务调用的接口,简单理解为可以通过网络去调用的函数.postman是一款前端调用工具,测试后端接口的时候往往是使用该工具去验证.在openstack的使用 ...

  4. js-使用JavaScript、jQuery两种方式实现全选/全不选

    html代码 <input type='checkbox' value="10" name="frust"/>苹果10元 <br/> & ...

  5. datatable使用笔记

    这是一个使用datatable的jsp文件实例,实现了点击单元格后编辑单元格所在行的内容. <%@ page pageEncoding="UTF-8"%> <!D ...

  6. ubuntu更换阿里源

    网上应该可以找到很多关于ubuntu源的设置方法,但是如果不搞清楚就随便设置的话,不仅不能起到应有的效果,还会由于一些问题导致apt不可用. 最正确的更换源的方法应该如系统提示的: ## a.) ad ...

  7. 关于 Python 入门的一些问题?

    一.用 python 能够做什么?解决什么问题? A1:理论上来说,计算机能做什么,python 语言就能让它做什么,也即 python能做什么. 数值计算.机器学习.爬虫.云相关开发.自动化测试.运 ...

  8. MySQL优化三 表结构优化

    由于MySQL数据库是基于行(Row)存储的数据库,而数据库操作 IO 的时候是以 page(block)的方式,也就是说,如果我们每条记录所占用的空间量减小,就会使每个page中可存放的数据行数增大 ...

  9. mysql加锁读

    Locking Reads 在同一个事务中,如果你先查询数据,随后对相关数据进行插入或修改,那么在标准的SLELECT中不会给出足够的保护.在你查询期间另一个事务可以更新或者删除相同的行.InnoDB ...

  10. Hadoop2.9.0安装

    参考 https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-common/SingleCluster.html 1.下载并解 ...