zoukankan      html  css  js  c++  java
  • HDU 1358 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 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
    题意描述:
    输入串的长度N(2 <= N <= 1 000 000)及长度为N的串
    计算并输出所有循环串的长度i以及该循环串中存在循环节的个数K(K>1)(或者循环周期)
    解题思路:
    对KMP中next[]数组的应用,重点在理解next[]数组的含义,即存储了从开头到斜上位置的串的前后缀的相似度。另外使用当前串的长度减去next[]末位得到的是该串最小循环节的长度。
    那么枚举从第三个字符开始到结尾的不同的串,判断当前串是否含有一个以上的循环节并且含有整数个循环节,是的话输出串的长度和循环节的个数即可。
    代码实现:
     1 #include<stdio.h>
     2 char s[1000010];
     3 int next[1000010];
     4 int n;
     5 void get_next(char t[]);
     6 int main()
     7 {
     8     int i,c,t=1;
     9     while(scanf("%d",&n),n != 0)
    10     {
    11         scanf("%s",s);
    12         get_next(s);
    13         printf("Test case #%d
    ",t++);
    14         for(i=2;i<=n;i++)
    15         {
    16             c=i - next[i];
    17             if(i/c > 1 && i%c==0 )
    18             printf("%d %d
    ",i,i/c);
    19         } 
    20         printf("
    ");
    21     }
    22     return 0;
    23 }
    24 void get_next(char t[])
    25 {
    26     int i,j;
    27     i=0;j=-1;
    28     next[0]=-1;
    29     while(i < n)
    30     {
    31         if(j==-1 || t[i]==t[j])
    32         {
    33             i++;
    34             j++;
    35             next[i]=j;
    36         }
    37         else//少个else 
    38             j=next[j];
    39     }
    40     /*for(i=0;i<=n;i++)
    41         printf("%d ",next[i]);
    42     printf("
    ");*/
    43 }


  • 相关阅读:
    php 下载文件
    thinkphp3.1 发送email
    微擎 plugin 时间插件 图片上传插件不显示 报错 影响下面执行
    Java中基本数据类型的对比记忆
    堆内存设置以及垃圾回收方式
    try--catch--finally中return返回值执行的顺序(区别)
    Java中的值传递和引用传递
    全面总结sizeof的用法(定义、语法、指针变量、数组、结构体、类、联合体、位域位段)
    10进制转换成16进制最简单的方法
    quartz 框架定时任务,使用spring @Scheduled注解执行定时任务
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/7344523.html
Copyright © 2011-2022 走看看