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. Markdown编辑器语法指南2

    人的一切痛苦, 本质上都是对自己的无能的愤怒. --王小波 1 Markdown编辑器的基本用法 1.1 代码 如果你只想高亮语句中的某个函数名或关键字,可以使用 `function_name()` ...

  2. The Euler function[HDU2824]

    The Euler functionTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...

  3. 常用Linux命令

    1.mkdir 建立目录 $ mkdir testdir 2.ls   列出目录下的内容的详细信息  ls -al testdir 3.cd  更换当前工作目录   cd testdir 4.pwd  ...

  4. 原生js实现autocomplete插件

    在实际的项目中,能用别人写好的插件实现相关功能是最好不过,为了节约时间成本,因为有的项目比较紧急,没充分时间让你自己来写,即便写了,你还要花大量时间调试兼容性.但是出于学习的目的,你可以利用闲暇时间, ...

  5. asp.net mvc 绑定客户端post过来的复杂JSON数据

    客户端代码如下: <input type="button" id="btnTest" value="测试" /><br / ...

  6. python学习之路 第三天

    1.set集合:去掉重复字段 set.difference()找出不同并创建一个新的集合,不改变原来集合: set.difference_update() 改变原来集合,剔除掉括号内容: set.di ...

  7. form 表单基础知识

    <form method=" name="one" action="http://www.battlenet.com.cn/zh/"> & ...

  8. js浮点乘除法运算不精确bug

    //除法函数,用来得到精确的除法结果 //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显.这个函数返回较为 精确的除法结果. //调用:accDiv(arg1,arg2 ...

  9. 使用Amoeba for mysql实现mysql读写分离

    Amoeba主要在应用层访问MySQL的时候充当query 路由功能,专注 分布式数据库 proxy 开发.座落与Client.DB Server(s)之间.对客户端透明.具有负载均衡.高可用性.Qu ...

  10. Eclipse安装scala

    Scala官方提供了三种插件,分别支持Eclipse.NetBeans和Intellij IDEA开发环境.要在Eclipse IDE下安装Scala插件:选择Eclipse的菜单项Help--> ...