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. Java 中的关键字和保留字

    关键字: Java 语言中已经事先定义好了的,有着特殊含义和用途 访问控制 类.方法和变量修饰符 程序控制 异常处理 包相关 基本类型 变量引用 public abstract break try i ...

  2. CS231n 2016 通关 第三章-SVM与Softmax

    1===本节课对应视频内容的第三讲,对应PPT是Lecture3 2===本节课的收获 ===熟悉SVM及其多分类问题 ===熟悉softmax分类问题 ===了解优化思想 由上节课即KNN的分析步骤 ...

  3. sqlplus 设置显示格式

    使用sqlplus查询显示结果,显示很乱,下面有种方法可以让她显示的更好看些.1.设置显示的宽度:设置前可以先查看当前宽度: SQL> show linesize;linesize 100SQL ...

  4. jackson 进行json与java对象转换 之三

    2.测试类,没用Junit,用Main()方法输出. package test; import java.io.IOException; import java.util.ArrayList; imp ...

  5. MPEG-PS封装格式

    据传输媒体的质量不同,MPEG-2中定义了两种复合信息流:传送流(TS:TransportStream)和节目流(PS:ProgramStream) PS文件分为3层:ps层(Program Stre ...

  6. ES02 变量、数组、对象、方法

    1 变量 1.1 变量的声明 利用var关键字来声明变量,例如: var a = 100; <!DOCTYPE html> <html> <head> <me ...

  7. mac 彻底卸载Paragon NTFS

    之前安装了paragon NTFS,试用期过了就卸载了,但是每天还是会提示“试用期已到期”,看着很烦. 百度了一下,发现网上的版本可能比较老了,和我的情况不太一样,但道理应该是一样的. 彻底删除方法: ...

  8. Mind Map - FreeMind

    FreeMind[1]是一款基于java的免费的脑图(mind mapping)制作与管理软件.FreeMind开发项目组正致力于使其成为一款高效率的工具.FreeMind具有一键“展开/折叠”功能以 ...

  9. Be a Smart Raftsman SGU475

    传送门 题目大意 有m+1个点,0是起点,m是终点,i-1到i有一条边,有一个船由0驶往m,不能返回,它在载重小于等于ci时通过第i条边消耗的时间为di否则为Di,现在有n个人,每个人体重为wi,上船 ...

  10. WOJ 43 电话邀请

    并查集缩点这个trick感觉明明用得很广泛,为什么以前都不知道…… 先把$m$条线路从小到大排个序,这样可以保证之前合并出来的一定是最小的,大的代价不会把小的覆盖掉. 维护两个并查集,一个用来缩点,另 ...