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. [收集]MVC3 HTML辅助方法集录

    1.跳转链接 @Html.ActionLink("linkText","actionName",routeValues,htmlAttributes) e.g& ...

  2. [No0000A5]批处理常用命令大全

    1.Echo 命令打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当前回显设置.语法echo [{on|off}] [message]Sample: echo off e ...

  3. JavaScript 汉字与拼音互转终极方案 附JS拼音输入法

    转:http://www.codeceo.com/article/javascript-pinyin.html 前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的 ...

  4. 用ajax查询天气

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

  5. struts2中值栈

    值栈中的两个逻辑部分: 1.属性context,为OGNLContext类型,实际为ActionContext对象的一个引用,本质是一个Map,里面存放的各种Map,如request,session, ...

  6. [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...

  7. archlinux安裝手记(Win10+Arch、GPT+UEFI、lvm)

    准备工具和设置制作启动盘连接网络硬盘分区规划分区LVM方案创建文件系统分区挂载激活lvm2钩子基础安装和配置配置镜像源基础系统安装fstab进入系统initramfs引导程序网络搭建使用环境用户管理用 ...

  8. 罗马数字转整数Leetcode13

    该题较为简单,但是需要知道罗马数字的表示以及取值.用了一下map,其实之前没用过,但仔细看了一下跟python的字典实际上差不多,扫了一遍函数就直接可以用了. class Solution { pub ...

  9. 网页中如何启用QQ交谈

    很多网友都会发现好多的网页中会有诸如,网页中如何启用QQ交谈? 1. 登录QQ, 打开网址:http://shang.qq.com/v3/widget.html 启用QQ通讯组件. 2. 选择组件样式 ...

  10. mybatis 一对一与一对多collection和association的使用

    在mybatis如何进行一对一.一对多的多表查询呢?这里用一个简单的例子说明. 一.一对一 1.association association通常用来映射一对一的关系,例如,有个类user,对应的实体 ...