KMP算法

Next[]函数深入理解,Next[]当前字符前匹配字符数,串长n-Next[i]=串内循环子串的长度p。

本题求子循环串内循环节数。

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<iostream>
#include<stdio.h>
using namespace std; char s[];
int Next[]; void getNext(int n){
int i=,j=-;
Next[]=-;
while(i<n){
if(j==-||s[i]==s[j]){
i++;j++;
Next[i]=j;
}
else j=Next[j];
}
} int main()
{
int n,k,t=,p;
//freopen("pe.txt","r",stdin);
while(cin>>n&&n>&&n<){
cin>>s;
getNext(n);
cout<<"Test case #"<<t++<<endl;
for(k=;k<=n;k++){
p=k-Next[k]; //循环节的长度
if(k%p==&&k/p>)
cout<<k<<' '<<k/p<<endl; //若字符串由循环节构成,第k个字母前完整循环节数
}
cout<<endl;
}
return ;
}

【KMP】Period的更多相关文章

  1. 【题解】PERIOD - Period [POJ1961] [SP263]

    [题解]PERIOD - Period [POJ1961] [SP263] 在进入这道题之前,我们需要了解 kmp 算法 不知道的童鞋可以去看一下Silent_EAG(一个可爱的女孩纸)的讲解. 关于 ...

  2. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  3. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  4. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  5. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  6. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  7. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

  8. 【kmp】似乎在梦中见过的样子

    参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...

  9. 【POJ2406】【KMP】Power Strings

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

随机推荐

  1. SHELL网络爬虫实例剖析--转载

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://nolinux.blog.51cto.com/4824967/1552472 前天 ...

  2. spring源码测试

    org.springframework.context.annotation.ComponentScanParserTests.componentScanWithAutowiredQualifier( ...

  3. ASP.Net MVC 之FileResult

    FileResult是一个基于文件的ActionResult,利用FileResult我们可以很容易地将从某个物理文件的内容响应给客户端.ASP.NET MVC定义了三个具体的FileResult,分 ...

  4. 《CSS网站布局实录》学习笔记(二)

    第二章 XHTML与CSS基础 2.1 XHTML基础 XHTML是网页代码的核心内容,标准XHTML代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  5. rpm命令数据库修复日志

    今天在linux安装软件过程中遇到了一个小坑,rpm数据库被破坏: 状况: #rpm -qa | grep rpm 返回: [解决方案] 删除旧数据库,然后重建数据库: 删除旧数据库: # rm /v ...

  6. Windows下查看端口被程序占用的方法

      有时安装程序时,会出现XX端口被占用的情况,可以通过CMD命令查看什么程序占用 1. netstat -ano 查看相应端口对应程序的PID 例如:   C:\>netstat -ano A ...

  7. Ubuntu 12.04更换显示器后显示“显示不支援”,只能进恢复模式工作

    以前用的一台14寸液晶,换成17寸后,进入系统显示器上“显示不支援”  .仔细观查,电脑硬盘自检能显示后,后面都是黑屏. 解决过程. 因为grub 启动菜单不能显示.盲按方向键,发现菜单里的其它项目可 ...

  8. Symfony2目录结构说明

    了解框架的目录结构是框架快速入门的一个途径,一个成熟的框架,每个功能模块都被划分存放在不同的目录. Symfony2一级目录结构: ├── app //这目录下包含了,配置文件(应用的配置文件会被im ...

  9. WebStorm 使用快捷键大全

    1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录的文件. 2. ctrl + j: 输出模板 3. ctrl + b: 跳到变量申明处 4. ctrl + al ...

  10. 微软SpeechRecognitionEngine

    API官网手册:http://msdn.microsoft.com/zh-cn/library/System.Speech.Recognition.SpeechRecognitionEngine(v= ...