(简单) 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 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.

  题目就是给一个字符串,然后让对从2到N的所有前缀字符串,找出里面的重复串。

  对于找重复串的问题,用扩展KMP就可以解决。然后就是对于每个前缀,可以证明后面的前缀的最小重复串的长度一定大于等于前面的,因为形成重复串的必要条件就是i+next1[i]>=length 这样的话如果前面不符合这个式子,后面就更不用说了,必须让i增大才可能。

代码如下:

// ━━━━━━神兽出没━━━━━━
//      ┏┓       ┏┓
//     ┏┛┻━━━━━━━┛┻┓
//     ┃           ┃
//     ┃     ━     ┃
//     ████━████   ┃
//     ┃           ┃
//     ┃    ┻      ┃
//     ┃           ┃
//     ┗━┓       ┏━┛
//       ┃       ┃
//       ┃       ┃
//       ┃       ┗━━━┓
//       ┃           ┣┓
//       ┃           ┏┛
//       ┗┓┓┏━━━━━┳┓┏┛
//        ┃┫┫     ┃┫┫
//        ┗┻┛     ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━

// Author        : WhyWhy
// Created Time  : 2015年07月18日 星期六 10时01分22秒
// File Name     : 1961.cpp

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int MaxN=1000006;

void EKMP_pre(int m,char s[],int next1[])
{
    int p=0,a=1,L;

    next1[0]=m;

    while(p+1<m && s[p]==s[p+1])
        ++p;

    next1[1]=p;

    for(int k=2;k<m;++k)
    {
        L=next1[k-a];
        p=next1[a]+a-(next1[a]!=0);

        if(k+L-1<p)
            next1[k]=L;
        else
        {
            ++p;

            while(p<m && s[p]==s[p-k])
                ++p;

            next1[k]=p-k;
            a=k;
        }
    }

    next1[m]=0;
}

int next1[MaxN];
char s[MaxN];
int N;

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int cas=1;
    int minn;

    while(~scanf("%d",&N) && N)
    {
        minn=1;
        scanf("%s",s);

        EKMP_pre(N,s,next1);

        printf("Test case #%d\n",cas++);

        for(int i=1;i<N;++i)
        {
            while(minn<=i && next1[minn]+minn<i+1)
                ++minn;

            if(minn<=i && (i-minn+1)%minn==0)
                printf("%d %d\n",i+1,(i-minn+1)/minn+1);
        }

        puts("");
    }

    return 0;
}

时间: 2024-12-20 01:45:37

(简单) POJ 1961 Period,扩展KMP。的相关文章

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 (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

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 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

(简单) POJ 2406 Power Strings,扩展KMP。

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 = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative inte

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher :G - Power Strings POJ - 2406(kmp简单循环节)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G - Power Strings POJ - 2406 题目: 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 = "abcdef". If we think of

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