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. Ubuntu安装 Sublime Text 及常用插件推荐

    之前一直在用 Code Blocks 这个IDE工具,可用着还是感觉不怎么好,于是在网上找到了一篇文章,上面介绍了不少IDE,我找到了Sublime Text 感觉挺不错的. 帖子地址: http:/ ...

  2. 粉丝投稿!从2月份的面试被拒到如今的阿里P7,说一说自己学java以来的经验!

    个人近期面试情况 今年二月以来,我的面试除了一个用友的,基本其他都被毙了,可以说是非常残酷的.其中有很多自己觉得还面的不错的岗位,比如百度.跟谁学.好未来等公司.说实话,打击比较大. 情况基本上是从三 ...

  3. Vue - MVVM模式及优点

    MVVM模式 视图层和数据层的双向绑定,让我们无需再去关心DOM操作的事情,更多的精力放在数据和业务逻辑上去 MVVM是Model-View-ViewModel的缩写.MVVM是一种设计思想. Mod ...

  4. Kali实现靶机远程控制

    任务一.使用netcat建立监听并连接 1.          在ubuntu开启本地监听,在kali中连接 2.          在kali中开启监听,在ubuntu中反弹连接 任务二.使用met ...

  5. JVM -- 类加载的过程

    类的加载过程? 一个Java文件从编码完成到最终执行,一般主要包括"编译"和"运行"两个过程.编译,即把我们写好的java文件,通过javac命令编译成字节码, ...

  6. 如何制作sitemaps网站地图

    如何制作sitemaps网站地图 1.0 前言 1.1 xml格式 1.2 常见问题 本文资料来源于网站 1.0 前言 Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页.最简 ...

  7. 手把手教你用C语言编写一个哈希表

    原文链接:https://www.swack.cn/wiki/001558681974020669b912b0c994e7090649ac4846e80b2000/001572849111298ae3 ...

  8. maven 无法导入ojdbc 的jar包 解决方法

    由于maven无法在线安装ojdbc包,所有先在我们需要手动导入. 准备环境: 1.系统需要配置好jdk以及maven环境. 2.ojdbc的jar包,记住jar的路径,我的路径是:E:\jdbc\o ...

  9. .net core 和 WPF 开发升讯威在线客服与营销系统:使用 WebSocket 实现访客端通信

    本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ...

  10. MongoDB的管理-深度长文

    (1) 启动和停止MongoDB: Ubuntu18下启动关闭MongoDB 启动MongoDB: 方法一: systemctl start mongod.service 方法二: 在MongoDB的 ...