Period
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 19817   Accepted: 9640

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 AK ,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

引用大佬的博客:简单的对next数组的应用,http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html
以及KMP算法的详细解释:https://blog.csdn.net/v_july_v/article/details/7041827

看了大佬的博客们写的代码:
#include<stdio.h>

int next[1000005];
int get_next(int n,char str[]){
int i=0,j=-1;
next[0]=-1;
//当循环到没有正确匹配的str时都令j=next[j]
//即上一次匹配到的最近 正确的 j 的下一个
//KMP算法,这样可以减少消耗
while(i<n){
if(j==-1||str[i]==str[j]){
i++;
j++;
next[i]=j;
}else{
j=next[j];
}
}
}
int main(){
int i,length,k=1;
int temp;
char str[1000005];
int count=1;
while(scanf("%d",&length)&&length){
scanf("%s",str);
get_next(length,str);
printf("Test case #%d\n",count);
count++;
for(i=1;i<=length;i++){
temp=i-next[i];
//temp为最短循环节的长度
if(i%temp==0&&i/temp>1)
printf("%d %d\n",i,i/temp);
//i%temp==0 是因为最小循环节需要被字符串长度整除
//同时 需要被整除
}
printf("\n");
}
return 0;
}

POJ_1961的更多相关文章

  1. KMP POJ 1961 Period

    题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...

随机推荐

  1. Reactor详解之:异常处理

    目录 简介 Reactor的异常一般处理方法 各种异常处理方式详解 Static Fallback Value Fallback Method Dynamic Fallback Value Catch ...

  2. TCP粘包问题的解决方案01——自定义包体

      粘包问题:应用层要发送数据,需要调用write函数将数据发送到套接口发送缓冲区.如果应用层数据大小大于SO_SNDBUF,那么,可能产生这样一种情况,应用层的数据一部分已经被发送了,还有一部分还在 ...

  3. 年轻人不讲武德来白piao我这个老同志

    朋友们好啊,我是码农小胖哥. 今天有个同学问我在吗,我说什么事? 给我发个截图,我一看!噢,原来是帮忙搞个定时任务,还是动态的. 他说了两种选择,一种是用DelayQueue,一种是用消息队列. 他说 ...

  4. 服务和进程管理及查看分区和cpu

    查看分区:cat /proc/partitions   [root@lbg init.d]# cat /proc/partitions major minor  #blocks  name       ...

  5. 如何通过iptables代理访问内网

    场景 A机器能够联通内网机器,B机器能够联通A机器,但是访问不到内网机器,场景是希望通过A机器能够转发直接联通局域网内的其它机器 机器IP 内网为172.0.0.x/24 A机器为172.0.0.10 ...

  6. Linux安装部署Redis(超级详细)

    前言 网上搜索了一筐如何在Linux下安装部署Redis的文章,各种文章混搭在一起勉强安装成功了.自己也记录下,方便后续安装时候有个借鉴之处. Redis版本 5.0.4 服务器版本 Linux Ce ...

  7. [原题复现+审计][RoarCTF 2019]Easy Calc(http协议走私、php字符串解析漏洞)

    简介  原题复现:  考察知识点:http协议走私.php字符串解析漏洞  线上平台:https://buuoj.cn(北京联合大学公开的CTF平台) 榆林学院内可使用信安协会内部的CTF训练平台找到 ...

  8. mysql密码问题

    这位老哥的: 版权声明:本文为CSDN博主「csdn-华仔」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/ ...

  9. OAuth 2.0授权框架详解

    目录 简介 OAuth的构成 refresh Token Authorization Code模式 隐式授权 Resource Owner 授权密码认证 Client 认证授权 github的OAut ...

  10. 安装Ubuntu16.04系统后分辨率底的问题

    问题描述:安装Ubuntu系统后有可能会遇到分辨率很低的问题,别着急,这是一个小问题. 解决方案:修改/etc/default/grub,打开终端用命令:sudo gedit /etc/default ...