• LA_3026_Period_(kmp)


    描述


    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1027

    给出一个长度为n的字符串,求它的每个前缀(前缀长度>=2)是否是循环的(循环次数>1),如果是,求出循环周期.

    分析


    我们先来看对于一个满足条件的循环的串.设其长度为l,一个循环节的长度为s,那么很明显,[1,l-s]与[s+1,l]这一对前后缀是相同的,并且l是s的整数倍.

    那么反过来呢?如果一个串的前后缀[1,l-s]与[s+1,l]相同,那么该串从第一位开始,每一位都和其右移s位的相同,如果l是s的整数倍,那么(l-s)就是s的整数倍,那么前缀可以分成整数个长度为s的字串,每一个字串都是右移s后重合的,那么这就是一个循环节为s的循环串.

    这样的话这两种定义就是等价的,所以我们直接用kmp求前后缀重叠就行了.

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=1000000+5;
     5 int n,kase;
     6 char p[maxn];
     7 int f[maxn];
     8 void get_fail(){
     9     f[0]=0; f[1]=0;
    10     for(int i=1;i<n;i++){
    11         int j=f[i];
    12         while(j&&p[i]!=p[j]) j=f[j];
    13         f[i+1]=p[i]==p[j]?j+1:0;
    14     }
    15 }
    16 int main(){
    17     while(scanf("%d",&n)&&n){
    18         scanf("%s",p);
    19         get_fail();
    20         printf("Test case #%d
    ",++kase);
    21         for(int i=2;i<=n;i++)
    22             if(f[i]>0&&i%(i-f[i])==0) printf("%d %d
    ",i,i/(i-f[i]));
    23         printf("
    ");
    24     }
    25     return 0;
    26 }
    View Code

    3026
    Period
    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 A K , that is A concatenated K times, for some string A. Of course, we also want to know
    the period K.
    Input
    The input file consists of several test cases. Each test case consists of two lines. The first one contains
    N (2 ≤ N ≤ 1000000) 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

  • 相关阅读:
    LabVIEW-水仙花数
    NRF51822自学笔记(一) 流水灯
    机器学习第四次作业
    4.K均值算法--应用
    机器学习第三次
    机器学习第二次专业
    算符优先分析
    自下而上语法分析
    递归下降语法分析
    LL(1)文法的判断,递归下降分析程序
  • 原文地址:https://www.cnblogs.com/Sunnie69/p/5542836.html
Copyright © 2020-2023  润新知