Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2258    Accepted Submission(s): 1117

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 <cstdio>
#include <cstring> using namespace std; const int maxn=; char str[maxn];
int N,fail[maxn]; void getfail()
{
int n=strlen(str);
fail[]=fail[]=;
for(int i=;i<n;i++)
{
int j=fail[i];
while(j&&str[i]!=str[j]) j=fail[j];
fail[i+]=(str[i]==str[j])?(j+):;
}
} int main()
{
int cas=;
while(scanf("%d",&N)!=EOF&&N)
{
scanf("%s",str);
printf("Test case #%d\n",cas++);
getfail();
for(int i=;i<=N;i++)
{
if(fail[i]&&(i%(i-fail[i])==))
{
printf("%d %d\n",i,i/(i-fail[i]));
}
}
putchar();
}
return ;
}

HDOJ 1358的更多相关文章

  1. 关于KMP算法的理解

    上次因为haipz组织的比赛中有道题必须用到KMP算法,因此赛后便了解了下它,在仔细拜读了孤~影神牛的文章之后有种茅塞顿开的感觉,再次ORZ. 附上链接http://www.cnblogs.com/y ...

  2. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  5. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. hdu 1358 Period

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 思路:Next数组的用法,在第i个位置上如果有i%(i-Next[i])==0的话最小循环节就是 ...

  7. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  8. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

  9. HDOJ(2056)&HDOJ(1086)

    Rectangles    HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...

随机推荐

  1. kali linux之窥看女神上网隐私(ettercap+wireshark+zenmap +dsniff)

    作者:小波 http://www.cnblogs.com/xiaobo-Linux/ 无聊就玩了玩,不要干一些坏事哟~~网上也有一些文章关于kali linux的,就实战了一番.kali是用的debi ...

  2. web测试与app测试的区别

    才开始做测试就接触的web端,后来也接触app端,所以在这里对于自己工作中所接触到的做一些总结(总要养成总结的好习惯). 对于web端和移动端app,功能方面的测试,例如测试设计方法这些都大同小异,都 ...

  3. BZOJ 1001: [BeiJing2006]狼抓兔子

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 20029  Solved: 4957[Submit][ ...

  4. [网站公告]数据库服务器IOPS跑满造成网站不能正常访问

    今年下午13:20-14:20左右,突增的访问量引发数据库服务器(阿里云RDS)IOPS跑满,造成大量请求执行缓慢,从而严重影响了网站的正常访问,给大家带来很大的麻烦,望大家谅解! 在出现故障时,当我 ...

  5. H5是什么,CSS3又是什么?

    经常有客户咨询说你们会做H5吗,就像这个,拿过来一看,一个上下滑动的贺卡,这已经成为了大部分人对H5的理解,甚至很多大公司都推出了制作这种动画的工具,可以快速生成此类页面.(其实,这就用到了一些CSS ...

  6. Spring Aspectj切入点语法定义

    在使用spring框架配置AOP的时候,pointcut"切入点" 例如定义切入点表达式 execution(* com.sample.service.impl..*.*(..)) ...

  7. 研究Extension和Category的一个例子

    Category: 1. 无法添加实例变量 2.将类的实现分散到多个不同文件或多个不同框架中. Extension: 1. 可以添加实例变量 注: 如果Category的头文件中也使用Extensio ...

  8. java script 基础知识

    方法:提供信息 函数去处理 给出一个结果 字符类型. 字符提前赋类型 int i=1; money x; float;datetime; decimal;var 万用类型 var a = 1; 1赋值 ...

  9. 关于Java中的继承和组合的一个错误使用的例子

    [TOC] 关于Java中的继承和组合的一个错误使用的例子 相信绝大多数人都比较熟悉Java中的「继承」和「组合」这两个东西,本篇文章就主要就这两个话题谈论一下.如果我某些地方写的不对,或者比较幼稚, ...

  10. theano broadcasting

    当我们使用函数对两个数组进行计算时,函数会对这两个数组的对应元素进行计算,因此它要求这两个数组有相同的大小(shape相同).如果两个数组的shape不同的话,会进行如下的广播(broadcastin ...