hdu 1358 Period 最小循环节

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358

分析:已知字符串,求其由最小循环节构成的前缀字符串。

/*Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3507    Accepted Submission(s): 1766

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
*/
#include <cstdio>
#include <cstring>
const int maxn = 1000000 + 100;
char a[maxn];
int next[maxn], n;
void getNext()
{
    int k = -1, j = 0;
    next[0] = -1;
    while(j < n){//遍历字符串
        if(k == -1 || a[j] == a[k]) next[++j] = ++k;
        else k = next[k];
    }
}

int main()
{
    int C = 0;
    while(~scanf("%d", &n) && n){
        scanf("%s", a);
        getNext();
        printf("Test case #%d\n", ++C);
        for(int i = 2; i <= n; i++){//遍历所有循环节长度
            int len = i;
            int cnt = len-next[len];
            if(cnt == 1){//循环节长度为1,直接输出
                printf("%d %d\n", len, len);
            }
            else if(cnt != len && (len % cnt) == 0){
                printf("%d %d\n", len, len/cnt);
            }
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-11-07 18:12:05

hdu 1358 Period 最小循环节的相关文章

HDU 1358 (求最小循环节)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题意:大概就是说给你一个字符串,然后找出能够循环的子串,输出子串某位置以及循环节的个数. 题解:用KMP算法得到next数组.然后,从i=2开始遍历,得到的i-next[i]为循环节大小. 如果对于i能够整除循环节,并且next数组的值不为0(如果为0,则代表不循环,但却可以整除) 则代表这个位置是可行的,位置为i,循环节个数为i/(i-next[i]) 代码如下: 1 #include<cs

hdu 3746 Cyclic Nacklace (KMP求最小循环节)

//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int len; char a[100010]; int next[100010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len) { if(j==-1||a[i]==a[j]) i

poj1961 Period kmp解决找字符串的最小循环节

/** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个),输出前缀字符串长度以及最大的循环周期: 思路: 参考自:http://www.cnblogs.com/chenxiwenruo/p/3546457.html 定理:假设S的长度为len,则S存在最小循环节,循环节的长度L为len-next[len],子串为S[0-len-next[len]-1]. (1)

HDU 3746 Cyclic Nacklace (KMP最小循环节)

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11264    Accepted Submission(s): 4821 Problem Description CC always becomes very depressed at the end of this month, he has check

HDU - 4333 Revolving Digits(拓展kmp+最小循环节)

1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> 312 -> 231 -> 123 因为:312>123, 231>123, 123=123 所以答案是:0 1 2 2.令str1=s,str2=s+s,然后str1作为子串,str2作为主串,进行扩展kmp求出str2[i...len2-1]与str1[0...len1-1]的最长

poj2406--Power Strings(KMP求最小循环节)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33178   Accepted: 13792 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

hdu 4291 矩阵幂 循环节

http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,而且矩阵的更神奇: g(g(g(n))) mod 109 + 7  最外层MOD=1e9+7  可以算出g(g(n))的循环节222222224,进而算出g(n)的循环节183120LL,然后由内而外计算即可 注释掉的是求循环节的代码 //#pragma comment(linker, "/STACK:102400000,102400000")

codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: dp[i]表示前i个字符需要的最小次数. dp[i] = min(dp[j]+w(j+1,i)); (0<=j<i); [j+1,i]如果存在循环节(自身不算),那么取最小的循环节x.w = digit((i-j)/x)+x; 否则w = i-j+1; 求一个区间最小循环节: 证明:http://w

UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)

题意:定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路:如果完全不循环,顶多就是类似于abc1这样咯,即n=1.但是如果循环出现了,比如abab,那就可以表示成(ab)2.还有一点,就是要使得n尽量大,那么当出现abababab时,应该要这么表示(ab)4,而不是(abab)2. 此题用神奇的KMP解决,也就是主要利用next数组.举例说明. 一般出现循环的都会大概是这样的:abcabcabc.而这样