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. 在windows下安装node-sass失败,提示\node-sass: Command failed,解决方案

    执行命令 yarn add node-sass@4.7.2 --dev --registry=https://registry.npm.taobao.org :报错 出现这个问题的原因一般是网络问题, ...

  2. ubuntu 文件编码格式 转换

    正在学习jquery,之前在windows下弄的编码到了 ubuntu下,乱码: 找到一个方法: iconv : 源文件:a.htm 格式:gbk: 目标:    a.html 格式:utf8: ic ...

  3. Shell脚本常用命令整理

    该笔记主要整理了一些常见的脚本操作命令,大致如下(持续补充中): 1. while.for循环 1. while.for循环 #!/bin/bash # while循环 v_start_date=${ ...

  4. martini-md参数(mdp文件)

    输入参数:一个典型的mdp文件 1 ; 2 ; STANDARD MD INPUT OPTIONS FOR MARTINI 2.x 3 ; Updated 02 feb 2013 by DdJ 4 ; ...

  5. tcpack--4延时ack

    TCP在收到数据后必须发送ACK给对端,但如果每收到一个包就给一个ACK的话会使得网络中被注入过多报文.TCP的做法是在收到数据时不立即发送ACK,而是设置一个定时器,如果在定时器超时之前有数据发送给 ...

  6. python 与 百度人脸识别api

    用python来做人脸识别代码量少 思路清晰, 在使用之前我们需要在我们的配置的编译器中通过pip       install baidu-aip  即可 from aip import AipFac ...

  7. mysql之binlog和各类日志介绍

    1.错误日志 错误日志作用: 记录MySQL的启动.停止信息以及在MySQL运行过程中的错误信息. 参数log_error(默认开启)  修改后重启生效 log_error=[path/[file_n ...

  8. mon到底能坏几个

    如果是在做ceph的配置,我们会经常遇到这几个问题 问:ceph需要配置几个mon 答:配置一个可以,但是坏了一个就不行了,需要配置只是三个mon,并且需要是奇数个 问:ceph的mon能跟osd放在 ...

  9. Java8 方法引用和构造方法引用

    如果不熟悉Java8新特性的小伙伴,初次看到函数式接口写出的代码可能会是一种懵逼的状态,我是谁,我在哪,我可能学了假的Java,(・∀・(・∀・(・∀・*),但是语言都是在进步的,就好比面向对象的语言 ...

  10. Java web项目 Jxl 读取excel 并保存到数据库,(从eclipse上移动到tomact服务器上,之路径更改,)

    最开始在eclipse中测试的时候,并没有上传到服务器上,后来发现,想要读取数据必须上传服务器然后把文件删除就可以了,服务器不可以直接读取外地的文件.用到jxl 1.上传到服务器 前端 <for ...