Period - HDU 1358(next求循环节)

题目大意:有一个长N的字符串,如果前缀Ni是一个完全循环的串(循环次数大于1),输出Ni和它循环了多少次。

 

分析:输入next的应用,求出来next数组直接判断Ni是否是完全的循环就行了,也就是Ni % next[i] == 0
下面代码

=======================================================================================================================

#include<stdio.h>
#include<string.h>

const int MAXN = 1e6+7;
const int oo = 1e9+7;

char s[MAXN];
int next[MAXN];

void GetNext(int N)
{
    int i=0, j=-1;
    next[0] = -1;

    while(i < N)
    {
        if(j==-1 || s[i]==s[j])
            next[++i] = ++j;
        else
            j = next[j];
    }
}

int main()
{
    int N, t=1;

    while(scanf("%d", &N), N)
    {
        int i;

        scanf("%s", s);

        GetNext(N);

        printf("Test case #%d\n", t++);
        for(i=2; i<=N; i++)
        {
            int k = i-next[i];
            if(next[i] && i % k == 0)
                printf("%d %d\n", i, i/k);
        }
        printf("\n");
    }

    return 0;
}
时间: 2024-11-03 21:40:08

Period - HDU 1358(next求循环节)的相关文章

Uva 12012 Detection of Extraterrestrial 求循环节个数为1-n的最长子串长度 KMP

题目链接:点击打开链接 题意: 给定一个字符串str 求字符串str的 循环节个数为 1-len 个的 最长子串长度 思路:套用kmp的性质 #include<string.h> #include<stdio.h> #include <iostream> using namespace std; #define n 1300 void getnext(char str[n],int next[n]){ int m=strlen(str); next[0]=next[1]

(KMP 1.4)hdu 3746 Cyclic Nacklace(使用next数组来求循环节的长度——求一个字符串需要添加多少个字符才能使该字符串的循环节的个数&gt;=2)

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

hud5451_求循环节加矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5451 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 找循环节解析链接:http://blog.csdn.net/ACdreamers/article/details/25616461 裸题链接:http://blog.csdn.net/chenzhenyu123456/article/details/48529039 1 #include <algorithm> 2 #i

Poj1961--Period(Kmp, Next数组求循环节长度 &amp;&amp; 出现次数)

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14657   Accepted: 6967 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

Brute-force Algorithm HDU - 3221(指数循环节 矩阵快速幂)

水题一道 推一下就是 f[n] = f[n - 1] + f[n - 2] 发现n很大 所以用矩阵快速幂就好了 还有P很大  那就指数循环节 一定要注意   这个条件 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #incl

Period HDU - 1358

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 (

(求循环节的个数)Power Strings -- poj -- 2406

链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&qu

hdu 3054 Fibonacci 找循环节的公式题

Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem Description We know the Fibonacci Sequence F1=1,F2=1,F3=2,F4=3,F5=5, ... Fx = Fx-1+Fx-2 We want to know the Mth number which has K consecutive "0&qu

HDU 1358

http://acm.hdu.edu.cn/showproblem.php?pid=1358 求某个前缀的周期,用Next求循环节的题目 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; char B[1000005] ; int Next[1000005],lenB ; void GetNext() { in