Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4325    Accepted Submission(s): 2087

Problem 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 file 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
 
Recommend
JGShining
 

也是一个KMP求周期的题目

与之前类似

不知道为啥next开成全局的就CE了,难道全局的就会和stl里的函数冲突。。

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std; int n;
char s[1000005]; int s_next[1000005]; int main() {
int cas = 1;
while(scanf("%d", &n) != EOF) {
if(n == 0) break; memset(s_next, 0, sizeof(s_next));
s_next[0] = -1;
scanf("%s", s);
int i = 0, j = -1;
while(i < n) {
if(j == -1 || s[i] == s[j]) s_next[++ i] = ++ j;
else j = s_next[j];
} printf("Test case #%d\n", cas ++); for(int i = 2; i <= n; i ++) {
int t = i - s_next[i];
if(i % t == 0 && i / t > 1) {
printf("%d %d\n", i, i / t);
}
}
printf("\n");
}
return 0;
}

HDU - 1358 - Period (KMP)的更多相关文章

  1. HDU 1358 Period(kmp简单解决)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. hdu 1358 Period (KMP求循环次数)

    Problem - 1358 KMP求循环节次数.题意是,给出一个长度为n的字符串,要求求出循环节数大于1的所有前缀.可以直接用KMP的方法判断是否有完整的k个循环节,同时计算出当前前缀的循环节的个数 ...

  3. hdu 1358:Period(KMP算法,next[]数组的使用)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. HDU 1358 Period (kmp判断循环子串)

    Period Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  5. HDU 1358 Period(KMP+最小循环节)题解

    思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...

  6. hdu 1358 Period(kmp求一个串的重复子串)

    题意:统计单串中从某个位置以前有多少重复的串 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string. ...

  7. Period(kmp)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. hdu 1686 Oulipo (kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. #include <iostream> #i ...

  9. POJ 1961 Period(KMP)

    http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...

随机推荐

  1. [BZOJ4316]小C的独立集(圆方树DP)

    题意:求仙人掌图直径. 算法:建出仙人掌圆方树,对于圆点直接做普通的树上DP(忽略方点儿子),方点做环上DP并将值直接赋给父亲. 建图时有一个很好的性质,就是一个方点在邻接表里的点的顺序正好就是从环的 ...

  2. [BZOJ 2743] 采花

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=2743 Algorithm: 此题询问区间内出现次数超过1个的数字 明显在线做无从下手,无 ...

  3. HDU 6017 Girls Love 233(多态继承DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6017 [题目大意] 给出一个只包含2和3的串,你可以花费两个智力值交换相邻的两个字符 问在智力值不 ...

  4. [CF441E]Valera and Number

    题意:给定$x,k,p$和一份伪代码,伪代码大致是循环$k$次,每次有$p\%$的概率把$x$乘$2$,有$(100-p)\%$的概率把$x$加$1$,问最后在二进制下$x$的末尾期望$0$个数 鸽了 ...

  5. 【高斯消元】【异或方程组】【bitset】bzoj1923 [Sdoi2010]外星千足虫

    Xor方程组解的个数判定: ——莫涛<高斯消元解Xor方程组> 使用方程个数判定:消去第i个未知数时,都会记录距第i个方程最近的第i位系数不为0の方程是谁,这个的max就是使用方程个数. ...

  6. 【kmp算法】模板

    void GetFail(char P[],int __next[])//__next[i]表示s[0]~s[i-1]的前缀中,最大长度相等的前后缀是多少 { __next[0]=-1; int le ...

  7. Eclipse编辑jsp卡死解决方案

    使用Eclipse编辑jsp.js文件时,经常出现卡死现象,在网上百度了N次,经过N次优化调整后,卡死现象逐步好转,具体那个方法起到作用,不太好讲.将所有用过的方法罗列如下: 1.取消验证 windo ...

  8. MathType插入空格

    公式太长,换行后加一些空格,继续录. 将鼠标定位到需要插入空格的位置,此时如果直接按空格键,你会发现并不能插入空格.正确的输入方法有两种: 方法一,在菜单栏中[样式]菜单下选择[文本],随后按空格键即 ...

  9. spring集成redis,集成redis集群

    原文:http://chentian114.iteye.com/blog/2292323 1.通过spring-data-redis集成redis pom.xml依赖包 <project xml ...

  10. MORMOT数据库连接池

    MORMOT数据库连接池 MORMOT封装了一堆的PROPS控件,用于连接各种数据库. MORMOT的封装是武装到了牙齿的,这堆PROPS控件居然数据库连接池也封装好了.这就为我们省了不少事,笔者非常 ...