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. android stagefright awesomeplayer 分析

    主要调用awesomeplay.cpp的函数来实现音视频等功能,可以说是对awesomeplay.cpp的封装,进一步抽象,然后提供给上层调用,主要的调用者是MediaPlayerService.cp ...

  2. Linux下安装oracle11g

    1.安装环境: Linux:Redhat Enterprise Linux 6.3 64位 Oracle:Oracle Database 11g for Linux x86-64 64位 2.修改操作 ...

  3. Sql server中根据object的定义查找object

    SELECT OBJECT_NAME(object_id) FROM sys.sql_modulesWHERE definition LIKE '%keyword to search%' 或者 SEL ...

  4. WindowsForm 流

    流 包括输入流代码 输出流代码 流: 输入流: string filename = openFileDialog1.FileName; //通过读入流进行文件读取 StreamReader sr = ...

  5. UVA 11214 Guarding the Chessboard

    题意: 皇后防御的范围是他所在横.竖.对角线,地图上的#为可以放旗子的地方.问最少放几个皇后能防守所有#. 分析: vis数组开4维,对应行.列.主对角线.副对角线 代码: #include < ...

  6. linux crontab设置

    cron来源于希腊单词chronos(意为“时间”),是linux系统下一个自动执行指定任务的程序.例如,你想在每晚睡觉期间创建某些文件或文件夹的备份,就可以用cron来自动执行. 服务的启动和停止 ...

  7. jQuery工具函数上

    1.字符串操作 <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...

  8. 简单C# 验证类

    using System; using System.Text.RegularExpressions; namespace bobomousecom.crm { /// <summary> ...

  9. HttpClient post 请求实例

    所需jar包: commons-codec-1.3.jar commons-httpclient-3.0.jar commons-logging-1.1.1.jar /** * */ package ...

  10. 简单方便又实用的在线作图工具:ProcessOn

    说到Visio大家都不陌生,虽然没有Word那么火,但很多业内朋友都在用,一用就是好多年,Visio是相对比较传统和专业的工具,新手在没人指导的情况下一般很难上手,下载和安装以及“授权”都是个问题,很 ...