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 多线程系列---JUC原子类(一)之框架

    根据修改的数据类型,可以将JUC包中的原子操作类可以分为4类. 1. 基本类型: AtomicInteger, AtomicLong, AtomicBoolean ;2. 数组类型: AtomicIn ...

  2. 使用pip一次升级所有安装的Python包(太牛了)

    import pip from subprocess import call for dist in pip.get_installed_distributions(): call("pip ...

  3. tomcat urlwrite报错

    十二月 26, 2017 2:15:30 下午 org.apache.catalina.core.ApplicationContext logINFO: org.tuckey.web.filters. ...

  4. DAY11-MYSQL多表查询

    一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 #建表 create table department( id int, name ) ); create table employ ...

  5. 用JS,做一个简单的计算器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8">   <title&g ...

  6. 用JS 写一个简单的程序,切换七彩盒子背景

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. UAC(User Agent Client) 和 UAS(User Agent Server)

    SIP协议采用Client/Server模型.每一个请求(Request)触发服务器的一个操作:每个操作被称为方法(Method):每个请求除了指明具体方法外,还携带了一系列的头域(Header fi ...

  8. session跨域共享

    www.maxomnis.com的index.php文件内容 <?phpsession_start();setcookie("user", "alex proter ...

  9. Luogu 3466 [POI2008]KLO-Building blocks

    BZOJ 1112. 题意相当于在一个长度为$k$的区间内选择一个数$s$使$\sum_{i = 1}^{k}\left | a_i - s \right |$最小. 很显然是中位数. 然后只要写一个 ...

  10. Luogu 4197 Peaks

    BZOJ 3545 带权限. 考虑离线,把所有边按照从小到大的顺序排序,把所有询问也按照从小到大的顺序排序,然后维护一个并查集和一个权值线段树,每处理一个询问就把比这个询问的$x$更小的边连上,具体来 ...