zoukankan      html  css  js  c++  java
  • HDU 1358 Period (kmp判断循环子串)

    Period

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 53   Accepted Submission(s) : 27

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    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
    

    题目大意:
    例如aabaabaabaab 在长度为2时,aa是连续两个a相等,则得到结果是2,aabaab是连续两个aab相等,得到结果是2;以此类推就知道意思了。不能是ababa的aba aba连续两个相等,是分开的,和poj2406那题很像 。
    解题思路:
    用公式(l % (l - next[l]) == 0) 来判断是否子串重复,还要判断是否( l / (l -  next[l]) >1) ;
     
     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 using namespace std;
     6 int n;
     7 char a[1000005];
     8 int nxt[1000005];
     9 void get_nxt()
    10 {
    11     nxt[0] = -1;
    12     int i = 0, j = -1;
    13     while (i < n) 
    14     {
    15         if (j == -1 || a[i] ==a[j]) 
    16         {
    17             nxt[++i] = ++j;
    18         }
    19         else 
    20         {
    21             j = nxt[j];
    22         }
    23     }
    24 }
    25 int main()
    26 {
    27     int k = 1;
    28     int i;
    29     while (cin >> n && n)
    30     {
    31         cin >> a;
    32         get_nxt();
    33         /*for (i = 0; i <= n; i++) cout << nxt[i];*/
    34         printf("Test case #%d
    ", k++);
    35         for (int i = 2; i <= n; i++) 
    36         {
    37             int cnt = i - nxt[i];
    38             if (i % cnt == 0 && i / cnt>1)//多次循环,且不是它本身 
    39             {
    40                 printf("%d %d
    ", i, i / cnt);
    41             }
    42         }
    43         printf("
    ");
    44     }
    45 }
  • 相关阅读:
    python3中类(class)的一些概念
    python 第三方库paramiko
    阿里云盘PC版开放了
    解决c#,wpf程序带环境安装包体积太大问题
    【利用静态网站传输数据】
    【.net】创建属于自己的log组件——改进版
    ThingsBoard 3.2.2 发布
    mac 安装pip2
    cocos creator2.4.3 内存优化总结
    cocos creator2.4.3 组件 节点 预制体
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271222.html
Copyright © 2011-2022 走看看