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. request.getContextPath()返回值问题

    转自:http://blog.sina.com.cn/s/blog_6cbe0cff0101j6jl.html request.getContextPath()是在开发Web项目时,经常用到的方法,其 ...

  2. maven项目添加jar包

    使用集成工具创建一个maven项目,如果需要添加开发包,只需去maven仓库找到对应的包,将配置信息加入pom.xml文件即可.这样,我们就再也不用到处寻找和下载jar包了. 用例:开发工具 STS ...

  3. Docker下配置KeepAlive支持nginx高可用

    案例子任务一.安装配置keepalived 步骤1:使用nginx镜像生成nginx-keep镜像 1) 启动nginx容器并进入 docker run -d --privileged nginx / ...

  4. springboot容器启动顺序之@Configuration ContextRefreshedEvent事件初始化 ApplicationRunner

    笔者最近遇到一个问题 我们根据自己业务需要  需要首次启动springboot项目时 把数据库数据同步至本地缓存(比如ehcache)但有一个要求 在缓存未载入成功  不允许有流量打入 一开始我们使用 ...

  5. java nio中,HeapByteBuffer与DirectByteBuffer的区别

    HeapByteBuffer,顾名思义,是写在jvm堆上面的一个buffer,底层的本质是一个数组,用类封装维护了很多的索引(limit/position/capacity等) DirectByteB ...

  6. 解决MySQL Workbench导出低版本MySQL时报错Unknown table ‘column_statistics’ in information_schema的问题

    在使用高版本MySQL Workbench或MySQL 8.0+版本提供的mysqldump.exe(实际高版本的MySQL Workbench使用的也是高版本的mysqldump.exe)来导出低于 ...

  7. BCC和libbpf的转换

    BCC和libbpf的转换 本文讲述如何将基于BCC的BPF应用转换为libbpf + BPF CO-RE.BPF CO-RE可以参见上一篇博文. 为什么是libbpf和BPF CO-RE? 历史上, ...

  8. 学习笔记之Python人机交互小项目一:名字管理系统

    2020是一个不平凡的一年,但即使挫折不断,我们每学期的课程实训也没有受到影响,仍旧如期实施.与往年不同的是,今年的实训老师是学校邀请的公司在职人员来给我们实训.今年实训的内容是Python语言,下面 ...

  9. [开源软件] 腾讯云Linux服务器一键安装LAMP/LNMP/LANMP环境 转

    本帖最后由 我本戏子 于 2015-8-13 22:00 编辑OneinStack是非常优秀的一键PHP/JAVA安装脚本,提供以下环境:lnmp(Linux + Nginx+ MySQL+ PHP) ...

  10. 工具用的好,下班回家早!5分钟玩转iTerm2!

    同时打开多个终端窗口,来回切换太麻烦! 能不能像IDEA一样,能够查看历史粘贴记录? 有没有办法一键登陆服务器? 工欲善其事,必先利其器!无论工作还是学习,选择好用的工具真的太重要了.今天就给大家介绍 ...