POJ 1961 Period (KMP)

解题思路:

利用next 数组的性质求解重复子串。循环节的长度为i - next[i];

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1000000 + 10;
char s[maxn];
int n;
int next[maxn];
int main()
{
    int kcase = 1;
    while(scanf("%d", &n)!=EOF)
    {
        if(n == 0) break;
        scanf("%s", s);
        next[0] = 0; next[1] = 0;
        for(int i=1;i<n;i++)
        {
            int j = next[i];
            while(j && s[i] != s[j]) j = next[j];
            next[i+1] = (s[i] == s[j]) ? j + 1 : 0;
        }
        printf("Test case #%d\n", kcase++);
        for(int i=2;i<=n;i++)
        {
            if(next[i] > 0 && i % (i - next[i]) == 0)
            {
                printf("%d %d\n", i, i / (i - next[i]));
            }
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-08-03 15:37:16

POJ 1961 Period (KMP)的相关文章

(简单) POJ 1961 Period,扩展KMP。

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 larges

poj 1961 Period(KMP求周期)

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

POJ 1961 Period

Period Time Limit: 3000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 196164-bit integer IO format: %lld      Java class name: Main For each prefix of a given string S with N characters (each character has an ASCII code bet

poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

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

poj 1961 Period 【KMP-next前缀数组的应用】

题目地址:http://poj.org/problem?id=1961 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 题目分析:给你一个字符串,最大长度1百万.输出是:以第1组样例解释,在aaa的字符串中,长度为2时,存在2个循环节a.当长度为3时,存在3个循环节a.以第二组样例解释,当长度为2时,存在2个循环节a.当长度为6时,存在2个循

poj 1348 Period(KMP)

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

poj 1961 Period 把主串的每一种前缀当作小主串,如果小主串的子串在小主串中叠加次数大于1,输出小主串长度及叠加次数。

#include<stdio.h> #define M 1000010 int i,n,next[M]; char s[M]; void getNext() { int j=-1; next[0]=-1; for(i=1;s[i];i++){ while(j!=-1&&s[j+1]!=s[i])j=next[j]; if(s[j+1]==s[i])j++; next[i]=j; } } int main() { int k=0; while(scanf("%d&quo

Period POJ - 1961

Period POJ - 1961 时限: 3000MS   内存: 30000KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 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 per

poj1961 Period(KMP)

C - Period Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1961 Appoint description: System Crawler (2016-05-10) Description For each prefix of a given str