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文本字符串处理

    第一种方法:#%*,#即截取变量前的字符(左向右截取),%表示截取后面字符(右向左截取),*匹配符 var=foodforthought.jpg ${varible##*string} 从左向右截取最 ...

  2. jQuery源代码 解析一 工具方法

    1. 外层沙箱以及命名空间$ 几乎稍微有点经验前端人员都这么做,为了避免声明了一些全局变量而污染,把代码放在一个"沙箱执行",然后在暴露出命名空间(可以为API,函数,对象): 2 ...

  3. TCP总结

      TCP协议   <计算机网络>谢希仁 及笔记 TCP 的那些事儿(上):http://coolshell.cn/articles/11564.html TCP 的那些事儿(下):htt ...

  4. SpringMVC04controller中定义多个方法

    public class MyController extends MultiActionController { // 新增 方法修饰符要是public public ModelAndView ad ...

  5. Asp.Net Api2 过滤器的使用

    1.注意: apiController控制器 对应的过滤器System.Web.Http.Filters.ActionFilterAttribute的过滤器 MVC的Controller控制器 对应的 ...

  6. 粗俗易懂的SQL存储过程在.NET中的实例运用

    整理了一下存储过程在项目中的运用,防止遗忘,便记录于此!存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中.用户通过指定存储过程的名字并给出参数( ...

  7. 关于ASP.Net 4.0的ClientID

    我们知道因为在原来的ASP.NET应用程序中使用服务端控件在生成ClientID的时,是很难控制的,特别是在嵌套的控件的时候,比如在多个嵌套Repeater中要控制某一个控件生成的html的ID属性, ...

  8. BestCoder Round 59 (HDOJ 5500) Reorder the Books

    Problem Description dxy has a collection of a series of books called “The Stories of SDOI”,There are ...

  9. 如何给report自定义page number

    问题描述: report在设置分页后会自动分页,但是有默认的page number,现在的问题是有时default page number不能满足我们的需求,此时就需要自定义page number. ...

  10. SQL Server 2008将数据导出为脚本

    之前我们要将一个表中的数据导出为脚本,那么只有在网上找一个导出数据的Script,然后运行就可以导出数据脚本了.现在在SQL Server 2008的Management Studio中增加了一个新特 ...