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. 进入mysql数据库修改密码

    mysql -hlocalhost -uroot -p #修改密码mysql> set password for root@localhost = password('root');#启动数据库 ...

  2. 第二章 进程同步(二)——> 重点

    2.4  进程同步 2.4.1  进程同步的基本概念 1.  两种形式的制约关系 (1)间接相互制约关系:互斥问题(往往是互斥设备)---是同步的特例 (2)直接相互制约关系:同步问题 注: 互斥问题 ...

  3. css3 知识点积累

    -moz-    兼容火狐浏览器-webkit-  兼容chrome 和safari1.角度  transform:rotate(30dge)  水平线与div 第四象限30度  transform: ...

  4. Java中常用修饰符浅谈

    一.public.protected.default和private修饰符的作用域 public:在java程序中,如果将属性和方法定义为 public 类型,那么此属性和方法所在的类和及其子类,同一 ...

  5. WDCP v3 安装

    ---已更新至3.0.3---经过近期的努力,wdCP_v3正式版终于可以和大家见面了v3功能预览1 底层完全重新架构,更安全稳定,省资源更高效2 安装更简单,快速与方便3 功能更强大和易扩展,且完美 ...

  6. java进阶(29)--HashMap集合

    一.HashMap简介 1.HashMap底层是哈希表结构,类似字典,初始化如下: 2.哈希表结构: 是一个数组+单向链表的结构体 数组:查询效率较高,随机增删效率很低 单向链表:在随机增删方面效率较 ...

  7. Token验证的流程及如何准确的判断一个数据的类型

    Token验证的流程: 1,客户端使用用户名跟密码请求登录:2,服务端收到请求,去验证用户名与密码:3,验证成功后,服务端会签发一个 Token,再把这个 Token 发送给客户端:4,客户端收到 T ...

  8. LeetCode-151-中等-翻转字符串里面的单词

    问题描述 给定一个字符串,逐个翻转字符串中的每个单词. 说明: 无空格字符构成一个 单词 . 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括. 如果两个单词间有多余的空格,将反转 ...

  9. js的函数-function

    function函数 function的英文是[功能],[数] 函数:职责:盛大的集会的意思 在js里,就是函数的意思.在Java里叫做方法. 定义函数 function fun(参数){ //函数体 ...

  10. Linux 防火墙基于 CentOS7 的防火墙操作命令

    防火墙服务操作命令 重启防火墙 systemctl restart firewalld 查看防火墙状态 systemctl status firewalld 开启.关闭.重启防火墙 # 开启 serv ...