zoukankan      html  css  js  c++  java
  • poj1961 & hdu1358 Period【KMP】

    Period
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 20436   Accepted: 9961

    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

    Source

    题意:

     求一个字符串的所有前缀的最短循环节。

    思路:

    首先,一个字符串要是可以由他的子串循环而成的话,那么这个字符串的长度一定是子串长度len的倍数。并且一定有S[len+1 ~ i] = S[1 ~ i- len]

    而KMP求出的nxt数组,表示的就是对于每一个i,S[i - nxt[i] + 1 ~ i] = S[1 ~ nxt[i]]

    因此,当i - nxt[i]能整除i时,S[1 ~ i - nxt[i]]就是S[1 ~ i]的最小循环元。

     1 #include <iostream>
     2 #include <set>
     3 #include <cmath>
     4 #include <stdio.h>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <map>
     8 using namespace std;
     9 typedef long long LL;
    10 #define inf 0x7f7f7f7f
    11 
    12 int n;
    13 const int maxn = 1e6 + 5;
    14 int nxt[maxn];
    15 char s[maxn];
    16 
    17 void getnxt()
    18 {
    19     nxt[1] = 0;
    20     for(int i = 2, j = 0; i <= n; i++){
    21         while(j > 0 && s[i] != s[j + 1]){
    22             j = nxt[j];
    23         }
    24         if(s[i] == s[j + 1])j++;
    25         nxt[i] = j;
    26     }
    27 }
    28 
    29 int main()
    30 {
    31     int cas = 1;
    32     while(scanf("%d", &n) != EOF && n){
    33         scanf("%s", s + 1);
    34         getnxt();
    35         printf("Test case #%d
    ", cas++);
    36         for(int i = 1; i <= n; i++){
    37             if(i % (i - nxt[i]) == 0 && i / (i - nxt[i]) > 1){
    38                 printf("%d %d
    ", i, i / (i - nxt[i]));
    39             }
    40         }
    41         printf("
    ");
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    增量更新代码步骤记录
    软件缺陷管理基本流程
    数据库语言(三):MySQL、PostgreSQL、JDBC
    eclipse的使用
    数据库语言(二):SQL语法实例整理
    windows下MySql没有setup.exe时的安装方法
    数学:完全独立于实际场景的情况下定义的概念,可以正确的描述世界
    数学语言和程序语言的对比:面向过程与面向集合&命题
    iOS开发之IMP和SEL(方法和类的反射)
    iOS之UIButton的normal和selected状态切换
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9821684.html
Copyright © 2011-2022 走看看