HDU 1358 Period

题目链接[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher E - Period

题意

给一字符串,求其所有完整循环的前缀与循环节的长度。

例:aaa

长度2前缀,循环节为a,个数为2

长度3前缀,循环节为a,个数为3

思路

kmp求出字符串前后缀重复数,遍历所有前缀子串进行下面操作:

字符串前后缀重复数next[L],则循环节的长度为L-L%next[L],如果L%循环节长度为0,则说明是完整循环,输出解。

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

char a[1000009];
int p[1000009];

void init(int m)
{
    memset(p, 0, sizeof(p));
    for(int i=1, k=0; i<m; i++)
    {
        while(k>0 && a[i]!=a[k]) k = p[k-1];
        if(a[i] == a[k]) k++;
            p[i] = k;
    }
}

void solve(int n)
{
    init(n);
    for(int i=1; i<n; i++)
    {
        int l = i+1-p[i];
        if(l != i+1 && (i+1)%l == 0)
            printf("%d %d\n", i+1, (i+1)/l);
    }
}

int main()
{
    int n, T = 1;
    while(~scanf("%d", &n) && n)
    {
        scanf("%s", a);
        printf("Test case #%d\n", T++);
        solve(n);
        printf("\n");
    }
    return 0;
}
时间: 2024-12-13 15:51:09

HDU 1358 Period的相关文章

poj1961 &amp; hdu 1358 Period(KMP)

poj 题目链接:http://poj.org/problem?id=1961 hdu题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 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

KMP + 求最小循环节 --- HDU 1358 Period

Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度的字符串是否是重复串,如果是,输出当前位置,并输出重复串的周期. analyse: 还是next数组的运用,详见上一篇博客. Time complexity: O(N) Source code:  /** this code is made by crazyacking* Verdict: Acce

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 ea

Hdu 1358 Period (KMP 求最小循环节)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和k. 解题思路: 打表算出next数组,然后对位置i求循环节,如果满足 i % (i - Next[i]) == 0 && Next[i] != 0,所对应的循环节(i - Next[i]), 循环次数是i / (i - Next[i]) 1 #include<cstdio> 2

hdu 1358 period KMP入门

Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输出k即周期数: 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 题目其实是来自于LA的..挺好的一道题,用的是原版的kmp.. 写写对KMP

(KMP 1.5)hdu 1358 Period(使用next数组来求最小循环节——求到第i个字符的循环节数)

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

HDU 1358 Period(kmp简单解决)

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

HDU - 1358 - Period (KMP)

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 ASCI

HDU 1358 Period 求前缀长度和出现次数(KMP的next数组的使用)

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