zoukankan      html  css  js  c++  java
  • [HDOJ1358]Period

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358

    Period

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4711    Accepted Submission(s): 2285


    Problem 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 file 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 #include <algorithm>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <iostream>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <climits>
    12 #include <cmath>
    13 
    14 using namespace std;
    15 
    16 const int maxn = 1000010;
    17 int nb;
    18 char b[maxn];
    19 int pre[maxn];
    20 
    21 void getpre(char *b, int *pre) {
    22     int j, k;
    23     pre[0] = -1;
    24     j = 0;
    25     k = -1;
    26     while(j < nb) {
    27         if(k == -1 || b[j] == b[k]) {
    28             j++;
    29             k++;
    30             pre[j] = k;
    31         }
    32         else {
    33             k = pre[k];
    34         }
    35     }
    36 }
    37 
    38 int main() {
    39     // freopen("in", "r", stdin);
    40     int kase = 1;
    41     while(~scanf("%d", &nb) && nb) {
    42         getchar();
    43         memset(b, 0, sizeof(b));
    44         gets(b);
    45         getpre(b, pre);
    46         printf("Test case #%d
    ", kase++);
    47         for(int i = 2; i <= nb; i++) {
    48             if(pre[i] > 0 && i %(i - pre[i]) == 0) {
    49                 printf("%d %d
    ", i, i / (i - pre[i]));
    50             }
    51         }
    52         printf("
    ");
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    TCP IP基础知识的复习
    Design Pattern: Singleton 模式
    解决Win7下安装VS2010不显示序列号框的两种方法
    字典树(Trie tree)
    在VS如何查看汇编代码
    使用模板实现编译期间多态
    一段c++代码小例子
    C++ 虚函数表解析
    C++问题:if( input.rdstate() & std::ios::failbit )
    Design Pattern: Adapter 模式 Class Adapter
  • 原文地址:https://www.cnblogs.com/kirai/p/4766239.html
Copyright © 2011-2022 走看看