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<cstring>
#include<cstdio>
#include<cmath> using namespace std; char s[];
int pn[];
int nextt[]; int main()
{
int l,z = ,i,j,k;
while(scanf("%d",&l)!=EOF,l)
{
printf("Test case #%d\n",++z);
scanf("%s",s+);
nextt[] = ;
pn[] = ;
for(i = ;i<=l;i++)
{
int t = nextt[i-];
while(t&&s[i]!=s[t+]) t = nextt[t];
if(s[i] == s[t+]) t++;
nextt[i] = t;
if(t == ) pn[i] = ;
else if(i-t == t/pn[t]) pn[i] = pn[t]+,printf("%d %d\n",i,pn[i]);
else pn[i] = ;
}
puts("");
}
}

注:hdu用next这个变量名会编译错误

hdu1358Period的更多相关文章

  1. Hdu-1358Period(KMP算法之next数组的应用)

    题解:对于串pattern来说,如果0~i-1这个位置中循环,那么i%(i-next[i])==0 ,循环次数为 i/(i-next[i]),循环长度为 i-next[i] 例如对于串ababab来说 ...

  2. HDU-1358-Period(KMP, 循环节)

    链接: https://vjudge.net/problem/HDU-1358#author=0 题意: For each prefix of a given string S with N char ...

  3. HDU--1358--KMP算法失配函数getfail()的理解--Period

    /* Name: hdu--1358--Period Author: 日天大帝 Date: 20/04/17 10:24 Description: 长度/向后移动的位数 = 出现的次数 kmp其实匹配 ...

随机推荐

  1. 学习asp.net比较完整的流程

    如果你已经有较多的面向对象开发经验,跳过以下这两步: 第一步 掌握一门.NET面向对象语言,C#或VB.NET 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.NE ...

  2. java 成神之路

    一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http://www.jcp.org/en/jsr/detail?id=133 http://i ...

  3. css模板之 web模板一

    效果 <html><head><style type="text/css">div.container{width:100%;margin:0p ...

  4. linux修改rm指令执行(数据安全)

    引用文章A:http://hi.baidu.com/jlusuoya/item/32ae398958088755840fabfb 引用介绍:将rm替换为mv. 引用文章B:http://blog.cs ...

  5. jq插件又来了,模拟select下拉框,支持上下方向键哦

    好久没来了,更新下插件, 这个原理就是利用的 input[type='hidden']和自定义属性data-value捆绑传值操作的,可是设置默认选项,回调等参数,代码不多,比较简单,吼吼 (func ...

  6. Android PorterDuff.Mode

    1.PorterDuff.Mode.CLEAR所绘制不会提交到画布上. 2.PorterDuff.Mode.SRC显示上层绘制图片 3.PorterDuff.Mode.DST显示下层绘制图片 4.Po ...

  7. java命令行运行带外部jar

    假设:java 代码路径为com.jdw.test,其中调用了外部jar包 则需要将jar包解压后,放入com同级目录 然后再com目录启动命令行 java com.jdw.test.HelloWor ...

  8. python执行外部程序模块pyshell

    写python程序的时候需要用到调用外部命令的模块,看了一下,还真不少,头疼,用着不顺手.根据官网推荐,我根据官网的subprocess模块定制了一个自己的shell,同时借鉴了github上面的sh ...

  9. Ubuntu配置OpenGL环境

    建立基本编译环境 sudo apt-get install build-essential 安装OpenGL Library sudo apt-get install libgl1-mesa-dev ...

  10. Oracle EBS-SQL (QA-2):检查接收未检验.SQL

    SELECT rsh.receipt_num                                                       收据号,         pov.vendor ...