HDU1358 Period【KMP】

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1358

题目大意:

给你长度为N的字符串s,求字符串s的循环前缀的长度和循环的次数。

例如:长度为8的字符串:"abababab"

长度为4的前缀"abab",循环前缀为"ab",循环2次

长度为6的前缀"ababab",循环前缀为"ab",循环3次

长度为8的前缀"abababab",循环前缀为"ab",循环4次

则输出:

4 2

6 3

8 4

思路:

KMP算法中Next[j]求的是第j个位置失配之后返回的匹配位置,即S[0]~S[Next[j]]与

S[j-1-Next[j]]~S[j-1]是相同的,对于当前前缀S[1]~S[i],如果i % (i-Next[i]) == 0,

则i - Next[i]就是最小重复子串,即循环节长度为i - Next[i]。循环次数为i / (i - Next[i])。

因为题目要求循环次数大于1,所以i / (i - Next[i]) > 1。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

char s[1000010];
int Next[1000010];

void GetNext(char *s)
{
    int i = 0,j = -1;
    Next[0] = -1;
    int len = strlen(s);
    while(i <= len)
    {
        if(j == -1 || s[i] == s[j])
        {
            i++,j++;
            Next[i] = j;
        }
        else
            j = Next[j];
    }
}

int main()
{
    int N,kase = 0;
    while(cin >> N && N)
    {
        cin >> s;
        GetNext(s);
//        for(int i = 0;i <= N; ++i)
//            cout << Next[i] << ' ';
        cout << "Test case #" << ++kase << endl;
        for(int i = 2; i <= N; ++i)
        {
            if(i % (i-Next[i]) == 0 && i/(i-Next[i])>1)
                cout << i << ' ' << (i/(i-Next[i])) << endl;
        }
        cout << endl;
    }

    return 0;
}
时间: 2024-10-08 06:28:24

HDU1358 Period【KMP】的相关文章

hdu-1358 Period 【kmp】

Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3443    Accepted Submission(s): 1727 Problem Description For each prefix of a given string S with N characters (each character has an ASCI

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 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 = "

POJ3461 Oulipo 【KMP】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22295   Accepted: 8905 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote

【KMP】【最小表示法】NCPC 2014 H clock pictures

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个针都是相同的,分别指向Ai,Bi(360°被分成360000小份),问能否将其中一个旋转和另一个重合. 题目思路: [KMP][最小表示法] 循环同构问题.可以写KMP,我懒得写KMP了就写了循环同构的最小表示法. 首先将Ai排序,然后求差(记得取模360000,WA了一次),接下来复制一遍开始匹配. A

【动态规划】【KMP】HDU 5763 Another Meaning

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007). 题目思路: [动态规划][KMP] 题目有点绕,看看样例就懂了.其实不用KMP直接用substr就能做. 首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]

HDU3746 Cyclic Nacklace 【KMP】

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

NYOJ327 亲和串 【KMP】

亲和串 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 最近zyc遇到了一个很棘手的问题:判断亲和串,以前判断亲和串的时候直接可以看出来,但现在不同了,现在给出的两字符串都非常的大,看的zyc头都晕了.于是zyc希望大家能帮他想一个办法来快速判断亲和串.亲和串定义:给定两个字符串s1和s2,如果能通过s1循环移动,使s2包含在s1中,那么我们就说s2是s1的亲和串. 输入 本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的

HDU4763 Theme Section 【KMP】

Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1114    Accepted Submission(s): 579 Problem Description It's time for music! A lot of popular musicians are invited to join us in t

HDU2594 Simpsons’ Hidden Talents 【KMP】

Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2798    Accepted Submission(s): 1055 Problem Description Homer: Marge, I just figured out a way to discover some of the