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. git clone 出现"error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with unexpected length was received."

    1. 最近用git pull几个大项目,总是报如下错误: error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with un ...

  2. 通过JS判断当前浏览器的类型

    通过JS判断当前浏览器的类型,对主流浏览器Chrome.Edge.Firefox.UC浏览器.QQ浏览器.360浏览器.搜狗浏览器的userAgent属性值来判断用户使用的是什么浏览器. 不同浏览器的 ...

  3. epoll内核源码详解(转 作者:赛罗·奥特曼 来源:牛客网)

    发现自己发的一篇面经后,很多小伙伴向我索要epoll的内核源码实现,那我就在牛客网发下这源码还有自己总结的流程. 另外 网上很多博客说epoll使用了共享内存,这个是完全错误的 ,可以阅读源码,会发现 ...

  4. Cache一致性和内存模型

    -------------------------------

  5. dp背包 面试题 08.11. 硬币

    https://leetcode-cn.com/problems/coin-lcci/ 硬币.给定数量不限的硬币,币值为25分.10分.5分和1分,编写代码计算n分有几种表示法.(结果可能会很大,你需 ...

  6. shell编程之trap命令

    trap command  signal trap捕获信号(软中断),command一般是linux命令 若为' '表示发生陷阱时为空指令,'-'表示发生陷阱时采用缺省指令 signal: HUP(1 ...

  7. PLSQL-解析XML

    DECLARE v_xmlclob CLOB := '<?xml version="1.0" encoding="UTF-8"?> <head ...

  8. 说说基于网络的五种IO模型

    # django不是一个异步框架 # tornado是异步的web框架 # 处理每秒大量的请求 # 个人理解的IO:就是应用层与内核驱动层的交互,这个过程无论从应用层到内核中,还是驱动层等待硬件层的数 ...

  9. php openssl 加密解密

    $config = array( "digest_alg" => "sha512", "private_key_bits" => ...

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

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