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. Meth | apt-get update ,upgarde 和dist-upgrade 的区别

    #sudo apt-get update 获得最近的软件包的列表:列表中包含一些包的信息,比如这个包是否更新过#sudo apt-get dist-upgrade 如果这个包没有发布更新,就不管它:如 ...

  2. ajax_get方式

    test_ajax_get.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  3. jdbc连接数据库和jdbc-odbc桥接方式连接数据库

    //这种方式为jdbc直接连接,需要添加jar文件 1 package com.howe2; import java.sql.*; public class test2 { public static ...

  4. 【iOS之轮播视图、自定义UIPageControl】

    基于UISrollView实现的无限循环轮播视图. 实现的思路:使用三个UIImageView不断循环利用,始终将最中间一个View显示在UIScrolView的contentSize上,每次滚动后, ...

  5. (转)html5 Placeholder属性兼容IE6、7方法

    使低版本浏览器支持Placeholder有很多方法,都不是很完美,或多或少有点问题,且有些原生支持的浏览器在获得焦点时会清空Placeholder提示.发现zhihu的解决方法不错,特记录下 wind ...

  6. CSS优先级总结(转载)

    样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...

  7. Eclipse利用代理快速安装插件

    在eclipse启动时增加以下参数: eclipse.exe -vmargs -DproxySet=true -DproxyHost=aProxyAddress -DproxyPort=aProxyP ...

  8. ant打包命令

    学习ant打包命令.发布到以上tomcat还未做集成部署,无法添加到jenkins中. http://blog.csdn.net/telnetor/article/details/7015935 ht ...

  9. 使用Canvas实现下雪功能

    示例代码: <html> <head> <meta http-equiv="Content-Type" content="text/html ...

  10. CSS 样式属性锦集

    ul#nav > Li 只有一个大于号,是指应用了#nav这个ID的下一级元素的儿子辈Li 元素定义的内容符合这个CSS代码定义的样式,但是孙子辈Li元素定义的内容就不符合这个CSS代码样式了, ...