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. 【QT】 Qt多线程的“那些事”

    目录 一.前言 二.QThread源码浅析 2.1 QThread类的定义源码 2.2 QThread::start()源码 2.3 QThreadPrivate::start()源码 2.4 QTh ...

  2. 1、线性DP 213. 打家劫舍 II

    https://leetcode-cn.com/problems/house-robber-ii/ //rob 0, not rob n-1 || not rob 0,not rob n-1 ==&g ...

  3. 1. 线性DP 152. 乘积最大子数组

    152. 乘积最大子数组  https://leetcode-cn.com/problems/maximum-product-subarray/ func maxProduct(nums []int) ...

  4. python之 socketserver模块的使用

    在我们正常的使用socket模块来写一个server的程序就会显得比较的复杂通常一般流程为 1.生成socket实例对象 2.绑定地址 3.开始监听 4.接收数据 一般demo为 # 服务器 impo ...

  5. diamond收集插件的自定义

    diamond是与graphite配合使用的一个数据收集的软件,关于这个配置的资料很多,使用起来也比较简单,详细的安装和配置会在后面的关于整套监控系统的文章里面写到,本篇是专门讲解怎么自定义这个数据收 ...

  6. TensorFlow_笔记

    Tensorflow 1.基本概念 TensorFlow是一个编程系统,使用图(graphs)来表示计算任务,图(graphs)中的节点称之为op(operation),一个op获得0个或多个Tens ...

  7. DevOps,你真的了解吗?

    与大数据和PRISM(NSA的监控项目之一),DevOps(开发运维)如今是科技人士挂在嘴边的热词,但遗憾的是,类似圣经,每个人都引用DevOps的只言片语,但真正理解并能执行的人极少.根据CA的一项 ...

  8. Javaweb项目页面实时显示后台处理结果

    http://www.cnblogs.com/dong-xu/p/6701271.html 此博文甚好,项目参照博主代码可实现. 前端页面: <%@ page language="ja ...

  9. day01-系统基础信息模块

    前言:2015年,由于大规模采用云平台虚拟机部署应用,每套应用系统的MySQl.接口机.WEB服务器.文件服务器需要经常进行版本升级.配置更新.安全补丁加固.iptables调整.配置核实,大量的手工 ...

  10. centos8 连接wifi

    从官网下载的6G多的iso安装后 #ifconfig -a 如下 enp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500l ...