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. CentOS Linux SVN服务器 配置用户目录访问 权限 Authorization failed

    SVN 修改 aurhz 文件设置用户目录访问权限格式: [/code] user=rw user 用户对code目录拥有读和写的权限. 但是访问 svn://192.168.1.59 的时候却提示A ...

  2. 如何解决Renesas USB3.0RootHub警告

    打开WINDOWS系统的[计算机管理]-[服务和应用程序]-[服务]-点击[Portable Device Enumerator Service]服务,设置为启动类型:自动(延迟启动).并点击&quo ...

  3. VIM操作快捷键

    i:插入光标前一个字符I:插入行首a:插入光标后一个字符A:插入行末o:向下新开一行,插入行首O:向上新开一行,插入行首M:光标移到中间行L:光标移动到屏幕最后一行行首G:移动到指定行{:按段移动,上 ...

  4. LVS之2---基于LVS负载均衡集群架构

    LVS之2---基于LVS负载均衡集群架构实现 目录 LVS之2---基于LVS负载均衡集群架构实现 ipvsadm software package Options 常用命令 保存及重载规则 内存映 ...

  5. 雅虎(ycsb)测试hbase(压测)

    一.下载ycsb 0.10包 https://github.com/brianfrankcooper/YCSB/releases/download/0.10.0/ycsb-0.10.0.tar.gz ...

  6. VUE项目性能优化实践——通过懒加载提升页面响应速度

    本文由葡萄城技术团队原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 最近我司因业务需求,需要在一个内部数据分析平台集成在线Excel功能,既然我 ...

  7. VMware虚拟机安装黑群晖DSM6.2 (转)

    安装DSM6.2.和安装5.1的过程大致相同,只是在虚拟机的配置时有所不同. 需要用到的工具 Roadkil's Disk Image – 写镜像工具:http://www.roadkil.net/p ...

  8. Hive表的基本操作

    目录 1. 创建表 2. 拷贝表 3. 查看表结构 4. 删除表 5. 修改表 5.1 表重命名 5.2 增.修.删分区 5.3 修改列信息 5.4 增加列 5.5 删除列 5.6 修改表的属性 1. ...

  9. linux下如何看系统日志及Out Of Memery

    1.一个Out Of Memery(OOM)导致的崩溃. linux系统经常会由于资源的问题导致自己的程序莫名被杀掉,可以从linux的系统日志中找到答案: vi /var/log/message - ...

  10. NOIP初赛篇——01计算机常识

    发展历史 年代划分 代别 年代 逻辑(电子)元件 第一代 1946-1958 电子管 第二代 1959-1964 晶体管 第三代 1965-1970 集成电路 第四代 1971-至今 大规模.超大规模 ...