Period
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 16776   Accepted: 8077

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.

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

Source


求每个前缀的最短循环节

错位的部分是个循环节
即i%(i-f[i])==0
 
再次想想从0开始问题 
 
跳到的那个位置之前的所有字符与当前失配字母前的相同数量个字母是相匹配的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+;
int n,f[N];
char p[N];
int main(){
int cas=;
while(scanf("%d",&n)!=EOF&&n){
scanf("%s",p);
f[]=f[]=;
for(int i=;i<n;i++){
int j=f[i];
while(j&&p[j]!=p[i]) j=f[j];
f[i+]=p[j]==p[i]?j+:;
}
printf("Test case #%d\n",++cas);
for(int i=;i<=n;i++)
if(f[i]>&&i%(i-f[i])==) printf("%d %d\n",i,i/(i-f[i]));
printf("\n");
}
}

POJ1961[KMP 失配函数]的更多相关文章

  1. poj2406 Power Strings(kmp失配函数)

    Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...

  2. POJ2406Power Strings[KMP 失配函数]

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 45005   Accepted: 18792 D ...

  3. HDU--1358--KMP算法失配函数getfail()的理解--Period

    /* Name: hdu--1358--Period Author: 日天大帝 Date: 20/04/17 10:24 Description: 长度/向后移动的位数 = 出现的次数 kmp其实匹配 ...

  4. 转载 - kmp next函数 kmp的周期问题,深入了解kmp中next的原理

    出处:http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html kmp next函数 kmp的周期问题,深入了解kmp中next的原理 ...

  5. luogu P5410 模板 扩展 KMP Z函数 模板

    LINK:P5410 模板 扩展 KMP Z 函数 画了10min学习了一下. 不算很难 思想就是利用前面的最长匹配来更新后面的东西. 复杂度是线性的 如果不要求线性可能直接上SA更舒服一点? 不管了 ...

  6. HDU 4763 Theme Section ( KMP next函数应用 )

    设串为str, 串长为len. 对整个串求一遍next函数,从串结尾开始顺着next函数往前找<=len/3的最长串,假设串长为ans,由于next的性质,所以找到的串肯定满足E……E这种形式, ...

  7. KMP&Z函数详解

    KMP 一些简单的定义: 真前缀:不是整个字符串的前缀 真后缀:不是整个字符串的后缀 当然不可能这么简单的,来个重要的定义 前缀函数: 给定一个长度为\(n\)的字符串\(s\),其 \(前缀函数\) ...

  8. POJ1961 KMP算法

    POJ1961 问题重述: 输入一个长度为l的字符串S,求所有S的由字串重复排列而成的前缀,并输出前缀的长度以及该前缀的最大重复度. AC代码: //Memory: 5700K Time: 641MS ...

  9. HDU 3336 Count the string ( KMP next函数的应用 + DP )

    dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...

随机推荐

  1. Linux解决乱码问题

    主要的关注点是本身文件的编码和Linux的编码是否相同, 如果不相同的话,那么修改 例如: 就OK了,如果还是有问题的话,那么就查看一下连接工具的编码是否设置的有问题. 一般情况下,主要注意这些问题, ...

  2. 转载:《TypeScript 中文入门教程》 17、注解

    版权 文章转载自:https://github.com/zhongsp 建议您直接跳转到上面的网址查看最新版本. 介绍 随着TypeScript和ES6里引入了类,现在在一些场景下我们会需要额外的特性 ...

  3. 【开发软件】推荐一款MAC OS X 下php集成开发环境mamp

      这里给大家推荐一款在mac上搭建WEB服务器环境的集成环境安装软件,非常的好用,需要的朋友可以拿去,不用谢 ^_^   之前苦于mac上搭建本地服务器之艰辛,找寻好久都没找到一款类似windows ...

  4. vmwawre 虚拟机优化配置

    vmware虚拟机如何设置不当的话会造成运行速度慢,并影响主机运行,甚至会出现死机. 一下是提高vmware虚拟机运行速度的几个技巧, 文章来自:http://blog.csdn.net/shanzh ...

  5. js获取网页高度

    网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...

  6. 浅谈RecyclerView(完美替代ListView,GridView)

    Android RecyclerView 是Android5.0推出来的,导入support-v7包即可使用. 个人体验来说,RecyclerView绝对是一款功能强大的控件. 首先总结下Recycl ...

  7. Android Studio 运行出现 Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.

    转载请标明出处: http://www.cnblogs.com/why168888/p/5978381.html 本文出自:[Edwin博客园] 我引用compile 'com.squareup.re ...

  8. iOS 最新版 CocoaPods 的安装流程

    iOS 最新版 CocoaPods 的安装流程1.移除现有Ruby默认源$gem sources --remove https://rubygems.org/2.使用新的源$gem sources - ...

  9. Linux备份ifcfg-eth0文件导致的网络故障问题

    今天在给一台操作系统为Oracle Linux Server release 5.7的服务器配置网络时,遇到了备份ifcfg-eth0配置文件,导致网卡无法绑定IP地址的情况.觉得是个有意思的案例,特 ...

  10. SQL SERVER 2005删除维护作业报错:The DELETE statement conflicted with the REFERENCE constraint "FK_subplan_job_id"

    案例环境: 数据库版本: Microsoft SQL Server 2005 (Microsoft SQL Server 2005 - 9.00.5000.00 (X64) ) 案例介绍: 对一个数据 ...