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. C# Span 源码解读和应用实践

    一:背景 1. 讲故事 这两天工作上太忙没有及时持续的文章产出,和大家说声抱歉,前几天群里一个朋友在问什么时候可以产出 Span 的下一篇,哈哈,这就来啦!读过上一篇的朋友应该都知道 Span 统一了 ...

  2. strace 使用文档

    strace -c 统计每一系统调用的所执行的时间,次数和出错的次数等. -d 输出strace关于标准错误的调试信息. -f 跟踪由fork调用所产生的子进程. -ff 如果提供-o filenam ...

  3. C++如何实现多态

    1.   什么是多态多态是C++中的一个重要的基础,面向对象编程语言中,接口的多种不同的实现方式即为多态.2.   多态带来的好处多态带来两个明显的好处:一是不用记大量的函数名了,二是它会依据调用时的 ...

  4. Markdown语法+Typora快捷键

    1. Markdown语法 1.1 代码块生成 // 对于代码块,使用"```+编程语言"即可生成书写对应代码块的区域 // JS代码块 ​```javascript // Jav ...

  5. .Net 开源项目 FreeRedis 实现思路之 - Redis 6.0 客户端缓存技术

    写在开头 FreeRedis 是一款继 CSRedisCore 之后重写的 .NET redis 客户端开源组件,以 MIT 协议开源托管于 github,目前支持 .NET 5..NETCore 2 ...

  6. 算法学习笔记:Tarjan算法

    在上一篇文章当中我们分享了强连通分量分解的一个经典算法Kosaraju算法,它的核心原理是通过将图翻转,以及两次递归来实现.今天介绍的算法名叫Tarjan,同样是一个很奇怪的名字,奇怪就对了,这也是以 ...

  7. 阿里技术专家深入讲解,SpringMVC入门到进阶,看这一篇就够了

    前言 SpringMVC是一个实现了Web MVC设计模式的轻量级Web框架.它与前辈Struts 2框架一样,都属于MVC框架,因为其使用和性能等方面比Struts 2更加优异,所以Spring M ...

  8. Zabbix监控笔记

    了解zabbix,有必要了聊一下监控系统相关内容 企业中常用的开源监视系统目前有 cacti.Nagios.Open-Falcon.zabbix.prometheus等 使用监控系统的目的在于 /1. ...

  9. 如何用Prometheus监控十万container的Kubernetes集群

    概述 不久前,我们在文章<如何扩展单个Prometheus实现近万Kubernetes集群监控?>中详细介绍了TKE团队大规模Kubernetes联邦监控系统Kvass的演进过程,其中介绍 ...

  10. CorelDRAW常用工具之涂抹工具

    CDR作为绘图软件或者说平面设计软件使用频繁的功能之一,就是为绘制好的图片进行涂抹混色. 1.基本操作 CorelDRAW平面设计软件的涂抹工具是在形状工具组里的,打开左侧工具栏"形状&qu ...