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. PHP学习笔记十三【二维数组】

    <?php //二维数组 $arr=array(array(1,2,3),array(4,5,6)); $arr1[0]=array(12,34,65); $arr1[1]=array(34,6 ...

  2. java 短信发送例子 tdy

    import java.io.IOException; import java.util.logging.Level;import java.util.logging.Logger;import or ...

  3. 关于tomcat的远程调试

    最近做项目开发发现,在本地运行好好的项目发布到测试服务器既然不好使了,很是郁闷,周围的大神们就给了一条明路:远程调试 查看了网上例子太多了,好像自己真的不会使用,就查了一些简单的资料发现其实很简单 下 ...

  4. express框架目录结构

    . ├── app.js ├── bin │   └── www ├── node_modules │   ├── body-parser │   ├── cookie-parser │   ├── ...

  5. php isset — 检测变量是否设置 foreach循环运用

    例子 $a = 336 ; $b = 33 ; function large($x,$y){ if((!isset($x))||(!isset($y))){ // echo "this fu ...

  6. 当each遇上list 的一些困惑

    在看手册函数的时候,看到each,如下: array each ( array &$array ) 返回数组中当前的键/值对并将数组指针向前移动一步 在执行 each() 之后,数组指针将停留 ...

  7. twisted 使用

    工欲善其事,必先利其器,我们先来进行 twisted 框架的安装,由于平时使用的都是 Windows 系统,那么下面我们就讲解下 Windows 下 twisted 框架的安装(1)下载 twiste ...

  8. C++类继承中的构造函数和析构函数 调用顺序

    思想: 在C++的类继承中,构造函数不能被继承(C11中可以被继承,但仅仅是写起来方便,不是真正的继承) 建立对象时,首先调用基类的构造函数,然后在调用下一个派生类的构造函数,依次类推: 析构对象时, ...

  9. keepalived 安装和配置

    第一步:安装 yum -y install keepalived 第二步:配置 /etc/keepalived/keepalived.conf ! Configuration File for kee ...

  10. js 异步流程控制之 avQ(avril.queue)

    废话前言 写了多年的js,遇到过最蛋疼的事情莫过于callback hell, 相信大家也感同身受. 业界许多大大也为此提出了很多不错的解决方案,我所了解的主要有: 朴灵 event proxy, 简 ...