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页面表单初始状态(主要是为了前台可以根据这个判断页面是否变动了)

    把页面表单状态记录到HiddenField中. 这里只提供后台代码, 前台逻辑根据需求自由定义. 存放值的ViewState: protected Dictionary<string, stri ...

  2. Android 关于屏幕适配

    android屏幕适配详解 官方地址:http://developer.android.com/guide/practices/screens_support.html 转自:http://www.c ...

  3. CSS基础要点概况

    1.CSS概述 1)css指层叠样式表 2)样式定义如何显示HTML元素 3)样式通常存储在样式表中 4)把样式添加到HTML4.0中,是为了解决内容与表现分离的问题 5)外部样式表可以极大提高工作效 ...

  4. .NET Framework 4.0-RequestValidationMode

    1.WebForm 先看如下 web.config 的代码: <system.web> <compilation debug="true" targetFrame ...

  5. C#抓取网页内容

    学习材料一 <C#抓取网页数据分析> 抓取Web网页数据分析 HttpWebRequest 和 HttpWebResponse 的应用

  6. Sublime Text 3中使用正则表达式删除空行

    Sublime Text 3 中使用正则表达式删除空行 Ctrl+H Find What: \n\n+ Replace With:\n

  7. ckeditor 使用手册

    CKEditor使用手册 在使用CKEditor过程中遇到了一些问题,现把它整理成手册,以便随时翻阅. 在页面<head>中引入ckeditor核心文件ckeditor.js <sc ...

  8. 配置managed server

    managed server往往是部署应用程序的server,所以最好在weblgoic上配置上managed server,不要把应用程序直接部署到admin server上. 一.受管服务器的创建 ...

  9. Router和History (路由控制)-backbone

    Router和History (路由控制) Backbone.Router担任了一部分Controller(控制器)的工作,它一般运行在单页应用中,能将特定的URL或锚点规则绑定到一个指定的方法(后文 ...

  10. 转:说说JSON和JSONP

    前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socke ...