zoukankan      html  css  js  c++  java
  • POJ 1961 Period( KMP )*

    Period

    Time Limit: 3000MS
    Memory Limit: 30000K

    Total Submissions: 12089
    Accepted: 5656

    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
                                            [Submit]   [Go Back]   [Status]   [Discuss]
     
    ::这道题跟poj2406差不多。题意:对于选定长度i(前i个字符,2<=i<=N),求出对应循环节k>1的情况
    比如: aabaabaabaab
    当i=2时, a ,a循环节有2个
    当i=6时, aab,aab循环节有2个
    当i=9时, aab,aab,aab循环节有3个
    当i=12时,aab,aab,aab,aab循环节有4个
    假设S的长度为len,则S存在循环子串,当且仅当,len可以被len - next[len]整除,最短循环子串为S[len - next[len]]
    证明见POJ 2406 Power Strings (KMP)
     
    代码:
       1: #include <iostream>
       2: #include <cstdio>
       3: #include <cstring>
       4: #include <algorithm>
       5: using namespace std;
       6: const int maxn=1e6;
       7: char s[maxn+10];
       8: int next[maxn+10];
       9:  
      10: void get_next(char s[],int len)
      11: {
      12:     int i=0,j=-1;
      13:     next[0]=-1;
      14:     while(i<len)
      15:     {
      16:         if(j==-1||s[i]==s[j]) {i++; j++; next[i]=j;}
      17:         else j=next[j];
      18:     }
      19: }
      20:  
      21: int main()
      22: {
      23:     int n,cas=1;
      24:     while(scanf("%d",&n)>0&&n)
      25:     {
      26:         scanf("%s",s);
      27:         printf("Test case #%d
    ",cas++);
      28:         get_next(s,n);
      29:         for(int i=2; i<=n; i++)//枚举长度i
      30:         {
      31:             if(i%(i-next[i])==0&&i!=(i-next[i]))
      32:                 printf("%d %d
    ",i,i/(i-next[i]));
      33:         }
      34:         printf("
    ");
      35:     }
      36:     return 0;
      37: }
  • 相关阅读:
    抽象类和接口
    【转载】Java 透析C/S和B/S结构
    java中的基本数据类型
    java之IO流详解(一)
    Yearning和inception搭建MySQL审核平台
    一个适合变化的产品部署集成包(nginx+jdk+tomcat+nodejs+mysql+redis+mongo+MYSQL主主(读写分离)集群建立+代码包+持续上线+备份)
    CPU计算密集型和IO密集型
    ansible结合SHELL搭建自己的CD持续交付系统
    JENKINS系统的安装部署
    DATAGUARD实施文档
  • 原文地址:https://www.cnblogs.com/zyx1314/p/3616630.html
Copyright © 2011-2022 走看看