Time Limit: 3000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u

Submit Status

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 A K ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input 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

__________________________________________________________________________________________

题目大意:求字符串的前缀中循环节的出现次数(大于1的)。

原理同上题一样,只是要注意next[]==-1时的判断。

__________________________________________________________________________________________

 1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4
5 using namespace std;
6 const int maxl=1e6+10;
7 int t,l,cas=0;
8 char s[maxl];
9 int next[maxl];
10 void getnext()
11 {
12 next[0]=-1;
13 for(int j,i=1;i<l;i++)
14 {
15 j=next[i-1];
16 while(s[i]!=s[j+1] && j>=0)j=next[j];
17 next[i]=s[i]==s[j+1]?j+1:-1;
18 }
19 }
20 int main()
21 {
22 while(scanf("%d",&l)==1 && l!=0)
23 {
24 printf("Test case #%d\n",++cas);
25 scanf("%s",s);
26 getnext();
27 for(int i=1;i<l;i++)
28 {
29 if(next[i]!=-1 && (i+1)%(i-next[i])==0)printf("%d %d\n",i+1,(i+1)/(i-next[i]));
30 }
31 printf("\n");
32 }
33 return 0;
34 }

POJ2961_kmp的更多相关文章

随机推荐

  1. jdbc编程学习之增删改查(2)

    一,enum类型的使用 在SQL中没有布尔类型的数据,我们都使用过布尔类型,当属性的值只用两种情况时.例如性别等.那在数据库对这些属性的值个数比较少时我们应该使用什么数据类型呢?SQL给我们提供了枚举 ...

  2. vue 导入.md文件(markdown转HTML)

    前言 刚接到这个需求的时候,觉得很简单(的确很简单)但是这玩意的坑真的也让人无奈. 网上找了很多的资料,都没有写出痛点(这就很难过了).通过实践并且在我们项目中平稳运行,想分享给后面的人 我的博客上也 ...

  3. Keil4 uVision软件生成hex文件

    keil4下载地址:http://www.pc6.com/softview/SoftView_236836.html 按图操作即可,注意文件夹选择. 1.选择工程,选择第一个new uvision p ...

  4. Docker本地镜像仓库搭建Nginx+BusyBox为例

    下载Busybox.Nginx镜像 docker pull busybox docker pull nginx 基于Busybox镜像创建容器,并在容器中做部分变更操作,生成新镜像 添加一些内容 正在 ...

  5. 二本非科班,秋招,实习,面试,offer之路

    不知不觉已经工作一年多的,我是2019年7月毕业的,但是如果算上实习就工作差不多两年了的吧. 最近不是刚刚过了圣诞节吗?然后又准备到元旦了,迎来2021年!在微信公众号上看到小部分公众号在总结2020 ...

  6. 使用Modbus4J进行RTU模式串口通信

    Modus协议是由MODICON(现为施耐德电气公司的一个品牌)在1979年开发的,是全球第一个真正用于工业现场的总线协议,应用非常广泛,可谓大名鼎鼎. 理论性的东西就不多介绍了,推荐一本书<M ...

  7. spark进行相同列的join时,只留下A与B关系,不要B与A

    一.问题需求: 近期需要做一个商品集合的相关性计算,需要将所有商品进行两两组合笛卡尔积,但spark自带的笛卡尔积会造成过多重复,而且增加join量 假如商品集合里面有: aa   aa bb   b ...

  8. 实现Vue的多页签组件

    在之前的博客中  关于vue的多页面标签功能,对于嵌套router-view缓存的最终无奈解决方法  有写过vue的多页签功能的解决方案 可以看到我当时那个多页签的组件还是比较简单 的,只有打开跟关闭 ...

  9. 【JavaWeb】XML 文件

    XML 文件 简介 XML 是可拓展的标记性语言. XML 的主要作用: 用来保存数据,且这些数据具有自我描述性: 作为项目或模块的配置文件: 作为网络数据传输的格式,但是现在以 JSON 格式为主. ...

  10. CSS 奇思妙想边框动画

    今天逛博客网站 -- shoptalkshow,看到这样一个界面,非常有意思: 觉得它的风格很独特,尤其是其中一些边框. 嘿嘿,所以来一篇边框特辑,看看运用 CSS,可以在边框上整些什么花样. bor ...