Period
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 14653   Accepted: 6965

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

  1. 3
  2. aaa
  3. 12
  4. aabaabaabaab
  5. 0

Sample Output

  1. Test case #1
  2. 2 2
  3. 3 3
  4.  
  5. Test case #2
  6. 2 2
  7. 6 2
  8. 9 3
  9. 12 4
    题意:求前缀的长度,以及其中最小循环节的循环次数 但是循环次数必须大于1
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX 1000100
  4. int next[MAX];
  5. char str[MAX];
  6. int a[MAX],b[MAX];
  7. int n,m,k;
  8. void getfail()
  9. {
  10. int i,j;
  11. next[0]=next[1]=0;
  12. for(i=1;i<n;i++)
  13. {
  14. j=next[i];
  15. while(j&&str[i]!=str[j])
  16. j=next[j];
  17. next[i+1]=str[i]==str[j]?j+1:0;
  18. }
  19. }
  20. void kmp()
  21. {
  22. int i,j;
  23. for(i=1;i<=n;i++)
  24. {
  25. k=i;
  26. if(k==(k-next[k]))//k-next[k]最小循环节长度
  27. continue;
  28. if(k%(k-next[k])==0)
  29. printf("%d %d\n",i,k/(k-next[k]));
  30. }
  31. printf("\n");
  32. }
  33. int main()
  34. {
  35. int i,t=1;
  36. while(scanf("%d",&n),n)
  37. {
  38. getchar();
  39. for(i=0;i<n;i++)
  40. scanf("%c",&str[i]);
  41. getfail();
  42. printf("Test case #%d\n",t++);
  43. kmp();
  44. }
  45. return 0;
  46. }

  

poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】的更多相关文章

  1. KMP POJ 1961 Period

    题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...

  2. [KMP求最小循环节][HDU1358][Period]

    题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...

  3. KMP + 求最小循环节 --- POJ 2406 Power Strings

    Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...

  4. hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)

    传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...

  5. HDU 3746 (KMP求最小循环节) Cyclic Nacklace

    题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...

  6. [KMP求最小循环节][HDU3746][Cyclic Nacklace]

    题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...

  7. poj 2406 Power Strings【字符串+最小循环节的个数】

                                                                                                      Po ...

  8. HDU 1358 Period 求前缀长度和出现次数(KMP的next数组的使用)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. poj 1961 Period 【KMP-next前缀数组的应用】

    题目地址:http://poj.org/problem?id=1961 Sample Input 3 aaa 12 aabaabaabaab 0 Sample Output Test case #1 ...

随机推荐

  1. PHP代码批量加密

    <?php error_reporting(E_ALL); ini_set('display_errors','1'); //批量加密码当前目录 $dirnow = getcwd(); $dir ...

  2. opencv 常用函数介绍

    ××××××××××××××××××××××××××××××××××××××× CvScalar imgmean,imgstd; double imgmax,imgmin; cvAvgSdv(img, ...

  3. Codeforces 543B Destroying Roads(最短路)

    题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...

  4. js 表达式与运算符 详解(下)

    比较运算符: > .>= .<. <=.  ==. !=. ===. !==. 比较运算符的结果都为布尔值 ==只比较值是否相等    而    ===比较的是值和数据类型都要 ...

  5. mysql数据类型——整型INT(m)

    1.整形分为四种 tinyint smallint mediumint int bigint 注意: 右侧的取值范围是在未加unsigned关键字的情况下,如果加了unsigned,则最大值翻倍,如t ...

  6. smarty中判断一个变量是否存在于一个数组中或是否存在于一个字符串中?

    smarty支持php的系统函数可以直接使用{if in_array($str, $arr) || strpos($str, $string)} yes {else} no{/if}

  7. Python Tutorial 学习(九)--Classes

    ## 9. Classes 类 Compared with other programming languages, Python's class mechanism adds classes wit ...

  8. Whitespace character

    In computer science, whitespace is any character or series of whitespace characters that represent h ...

  9. Js使用word书签填充内容

    Js使用word书签填充内容 1.在模板文件中需要填充的地方插入书签 填充内容为:(|光标所在处) 填写书签名,点击添加完成: 2.使用js打开模板,获取书签位置,填充数据: function pri ...

  10. myeclipse spket spket-1.6.23.jar 破解安装教程

    一年前安装文档就写过了,今天写破解文档,本来开发js/ext是想用aptana的,但是安装包100多M,我还是用spket吧(才11M),这个需要破解一下license,否则用不了. 一 安装教程如下 ...