poj 1961 Period 把主串的每一种前缀当作小主串,如果小主串的子串在小主串中叠加次数大于1,输出小主串长度及叠加次数。

  1. #include<stdio.h>
  2. #define M 1000010
  3. int i,n,next[M];
  4. char s[M];
  5. void getNext()
  6. {
  7. int j=-1;
  8. next[0]=-1;
  9. for(i=1;s[i];i++){
  10. while(j!=-1&&s[j+1]!=s[i])j=next[j];
  11. if(s[j+1]==s[i])j++;
  12. next[i]=j;
  13. }
  14. }
  15. int main()
  16. {
  17. int k=0;
  18. while(scanf("%d",&n),n){
  19. scanf("%s",s);
  20. printf("Test case #%d\n",++k);
  21. getNext();
  22. for(i=1;i<=n;i++)
  23. if(i%(i-next[i-1]-1)==0&&next[i-1]>-1)
  24. printf("%d %d\n",i,i/(i-next[i-1]-1));
  25. puts("");
  26. }
  27. return 0;
  28. }
时间: 2024-12-18 22:11:38

poj 1961 Period 把主串的每一种前缀当作小主串,如果小主串的子串在小主串中叠加次数大于1,输出小主串长度及叠加次数。的相关文章

POJ 1961 Period

Period Time Limit: 3000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 196164-bit integer IO format: %lld      Java class name: Main For each prefix of a given string S with N characters (each character has an ASCII code bet

(简单) POJ 1961 Period,扩展KMP。

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 larges

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

题目地址:http://poj.org/problem?id=1961 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百万.输出是:以第1组样例解释,在aaa的字符串中,长度为2时,存在2个循环节a.当长度为3时,存在3个循环节a.以第二组样例解释,当长度为2时,存在2个循环节a.当长度为6时,存在2个循

poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

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

poj 1961 Period(KMP求周期)

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 15963   Accepted: 7644 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

POJ 1961 Period (KMP)

解题思路: 利用next 数组的性质求解重复子串.循环节的长度为i - next[i]; #include <iostream> #include <cstring> #include <cstdlib> #include <vector> #include <cmath> #include <algorithm> #include <cstdio> using namespace std; const int maxn

Period POJ - 1961

Period POJ - 1961 时限: 3000MS   内存: 30000KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 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 per

POJ 3358 Period of an Infinite Binary Expansion( 数论好题 + 欧拉定理 + 欧拉函数 )

POJ 3358 Period of an Infinite Binary Expansion( 数论好题 + 欧拉定理 + 欧拉函数 ) #include <cstdio> #include <cstring> #include <algorithm> #include <algorithm> using namespace std; typedef long long LL; LL fac[ 100000 ], pf; LL gcd( LL a, LL

hdu3065 病毒侵袭持续中 AC自动机入门题 N(N &lt;= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。

/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数. 思路:ac自动机做发,val标记每一个病毒串编号,通过print函数统计每一个病毒出现的次数. AC自动机好文章:http://www.cppblog.com/menjitianya/archi