• POJ 1961 Period KMP


    Period
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 9758   Accepted: 4461

    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.

    Input

    The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the
    number zero on it.

    Output

    For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

    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 /* 功能Function Description:     poj-1961
     2    开发环境Environment:          DEV C++ 4.9.9.1
     3    技术特点Technique:
     4    版本Version:
     5    作者Author:                   可笑痴狂
     6    日期Date:                      20120814
     7    备注Notes:
     8     题意:给定一个字符串,问这个字符串的所有前缀中,有哪些前缀可以由某个串重复k次组成,
     9     并且这个k最大是多少。这个k需要大于1。
    10     --------KMP
    11 */
    12 #include<cstdio>
    13 #include<string>
    14 #include<cstdlib>
    15 
    16 void get_next(int *next,char *s,int n)
    17 {
    18     int i,j;
    19     i=0;
    20     j=-1;
    21     next[0]=-1;
    22     while(s[i])
    23     {
    24         if(j==-1||s[i]==s[j])
    25         {
    26             ++i;
    27             ++j;
    28             next[i]=j;
    29         }
    30         else
    31             j=next[j];
    32     }
    33 }
    34 
    35 int main()
    36 {
    37     int n,i,T=1;
    38     int *next;
    39     char *s;
    40     while(scanf("%d",&n),n)
    41     {
    42         next=new int[n+1];
    43         s=new char[n+2];
    44         scanf("%s",s);
    45         get_next(next,s,n);
    46         printf("Test case #%d\n",T++);
    47         for(i=1;i<=n;++i)
    48         {
    49             if(i%(i-next[i])==0&&i!=i-next[i])         //i-next[i]为循环节长度
    50                 printf("%d %d\n",i,i/(i-next[i]));
    51         }
    52         printf("\n");
    53     }
    54     return 0;
    55 }
    功不成,身已退
  • 相关阅读:
    HDU1164_Eddy's research I_素数筛选法
    HDU1213_How Many Tables
    HDU1049_数学水题
    HDU1286_找新朋友_筛选法
    HDU1128_Self Numbers_筛选法
    HDU3199_Hamming Problem_类似丑数
    HDU1297_Children’s Queue_递推题
    HDU1856_More is better
    HDU1397_Goldbach's Conjecture_素数筛选法
    HDU1272_并查集
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2637430.html
Copyright © 2020-2023  润新知