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 A K , that is A concatenated K times, for some string A. Of course, we also want to know the period K.
InputThe 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.

OutputFor 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]就可以了
 #include<stdio.h>
#include<string.h>
int Next[],n;
char p[]; void prekmp() {
int i,j;
j=Next[]=-;
i=;
while(i<n) {
while(j!=-&&p[i]!=p[j]) j=Next[j];
Next[++i]=++j;
}
} int main() {
int Case=;
//freopen("out","r",stdin);
while(~scanf("%d",&n)) {
if(n==) break;
scanf("%s",p);
prekmp();
printf("Test case #%d\n",++Case);
for(int i=;i<=n;i++) {
int l=i-Next[i];
// printf("%d***%d\n",l,Next[i]);
if(i%l==&&i/l>) printf("%d %d\n",i,i/l);
}
printf("\n");
}
}

kuangbin专题十六 KMP&&扩展KMP HDU1358 Period的更多相关文章

  1. kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  2. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  5. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  6. kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条

    一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...

  8. kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence

    Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...

随机推荐

  1. VxVM vxsnap ERROR V-5-1-0 Volume cannot be linked due to size/regionsize incompatibility

    在做vxsnap addmir时报错如下: #> vxsnap -g OLS_DATA_DG -b addmir OLS_DATA_ACC_P mirvol=OLS_DATA_ACC_SM1 V ...

  2. mysql如何开启远程连接(默认未开启,即使密码正确,仍然无法访问)

    mysql如何开启远程连接 | 浏览:1846 | 更新:2015-03-11 20:19 1 2 3 4 5 6 分步阅读 百度经验:jingyan.baidu.com 大家在公司工作中,经常会遇到 ...

  3. leetcode482

    这道题主要使用了C++的几个API,大小写转化,字符串替换.其余的逻辑都比较简单.而且经查资料,string类字符串拼接的速度使用+=的速度是很快的.以下代码,也是用的+=来拼接字符串. string ...

  4. 安卓SQLite数据库操作(下)

    在安卓开发中,数据库的操作无非就是增删改查.那么,这里我们通过例子来学习这四个操作. 我们先看代码吧.具体讲解后面说. 布局文件 activity_main.xml <LinearLayout ...

  5. JMS编程模型

    (1) ConnectionFactory创建Connection对象的工厂,针对两种不同的jms消息模型,分别有QueueConnectionFactory和TopicConnectionFacto ...

  6. Spring4新的javaConfig注解

    1.@RestController spring4为了更方便的支持restfull应用的开发,新增了RestController的注解,比Controller注解多的功能就是给底下的RequestMa ...

  7. Jquery前端选择器

    ----------------------祖先后代选择器------------------------------ 1.祖先 后代:根据一个元素可以取得指定的所有子元素(不管中间有多少后代)$(& ...

  8. [xdoj1007]易碎的鸟蛋(dp)

    解题思路:f[n,m]表示n层楼.m个鸡蛋时所需要的最小次数,则 转移方程为:f[n,m] = min{ 1+max(f[i-1,m-1], f[n-i,m]) | i=1..n }初始条件:f[i, ...

  9. WINFORM 无边框窗体 阴影与移动

    //窗体移动API[DllImport("user32.dll")]public static extern bool ReleaseCapture();[DllImport(&q ...

  10. opencv reshape函数说明

    转自http://blog.csdn.net/yang6464158/article/details/20129991 reshape有两个参数: 其中,参数:cn为新的通道数,如果cn = 0,表示 ...