Problem Description

For each prefix with length P of a given string S,if

S[i]=S[i+P] for i in [0..SIZE(S)-p-1],

then the prefix is a “period” of S. We want to all the periodic prefixs.

 Input

Input contains multiple cases.

The first line contains an integer T representing the number of cases. Then following T cases.

Each test case contains a string S (1 <= SIZE(S) <= 1000000),represents the title.S consists of lowercase ,uppercase letter.

 Output

For each test case, first output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of periodic prefixs.Then output the lengths of the periodic prefixs in ascending order.

 Sample Input

4
ooo
acmacmacmacmacma
fzufzufzuf
stostootssto

 Sample Output

Case #1: 3
1 2 3
Case #2: 6
3 6 9 12 15 16
Case #3: 4
3 6 9 10
Case #4: 2
9 12
 
题意:求出一个字符串的所有可能循环节的长度
思路:kmp的运用,题目的意思不是很清楚 j=next[j] 就是次大的匹配个数
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<iostream>
  5. #include<string>
  6. #include<vector>
  7. #include<stack>
  8. #include<bitset>
  9. #include<cstdlib>
  10. #include<cmath>
  11. #include<set>
  12. #include<list>
  13. #include<deque>
  14. #include<map>
  15. #include<queue>
  16. #define ll long long int
  17. using namespace std;
  18. inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
  19. inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
  20. int moth[]={,,,,,,,,,,,,};
  21. int dir[][]={, ,, ,-, ,,-};
  22. int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
  23. const int inf=0x3f3f3f3f;
  24. const ll mod=1e9+;
  25. int nextt[];
  26. void getnext(string s){
  27. nextt[]=; int len=s.length();
  28. for(int i=,j=;i<=len;i++){
  29. while(j>&&s[i-]!=s[j]) j=nextt[j];
  30. if(s[i-]==s[j]) j++;
  31. nextt[i]=j;
  32. }
  33. }
  34. int main(){
  35. ios::sync_with_stdio(false);
  36. int t;
  37. cin>>t;
  38. int w=;
  39. while(t--){
  40. string s;
  41. cin>>s;
  42. getnext(s);
  43. int len=s.length();
  44. queue<int> q;
  45. int j=nextt[len];
  46. while(j>){
  47. q.push(j);
  48. j=nextt[j];
  49. }
  50. q.push();
  51. bool f=;
  52. cout<<"Case #"<<++w<<": "<<q.size()<<endl;
  53. while(!q.empty()){
  54. int temp=q.front();
  55. q.pop();
  56. if(f){
  57. cout<<len-temp;
  58. f=;
  59. }else{
  60. cout<<" "<<len-temp;
  61. }
  62. }
  63. cout<<endl;
  64. }
  65. return ;
  66. }

FZU - 1901 Period II(kmp所有循环节)的更多相关文章

  1. FZU 1901 Period II(KMP循环节+公共前后缀)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 题目大意:题目大意求出所有p满足s[i]=s[i+p](i<=len-p) 解题思路: 其实就是要 ...

  2. [FZU 1901]Period II KMP

    For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...

  3. Fzu Problem 1901 Period II (kmp)

    题目链接: Problem 1901 Period II 题目描述: 给出一个串,满足长度为p的前缀和长度为p的后缀相等的p的个数,输出p的个数,和p分别是多少? 解题思路: 对kmp的next数组的 ...

  4. FZU - 1901 Period II (kmp)

    传送门:FZU - 1901 题意:给你个字符串,让你求有多少个p可以使S[i]==S[i+P] (0<=i<len-p-1). 题解:这个题是真的坑,一开始怎么都觉得自己不可能错,然后看 ...

  5. HDU 1358 Period(KMP+最小循环节)题解

    思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...

  6. FZU 1901 Period II(KMP中的next)题解

    题意:给你一串字符串,问你前后缀相同情况有几种,并输出后缀位置(?这里一直没看懂length是什么,但是这样理解答案也对,然后还要加上本身长度) 思路:这里好好讲讲next的用法.我们都知道next代 ...

  7. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  8. FZU1901 Period II —— KMP next数组

    题目链接:https://vjudge.net/problem/FZU-1901  Problem 1901 Period II Accept: 575    Submit: 1495Time Lim ...

  9. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

随机推荐

  1. MySQL Limit优化(转)

    原文:http://bbs.landingbj.com/t-0-240894-1.html 首先,我们看一个分页SQL: SELECT time,pageFROM `l_not_200_page`WH ...

  2. 【Python3练习题 019】 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

    后一个分数的分子=前一个分数的分子+分母,后一个分数的分母=前一个分数的分子,循环个20次就有结果.注意,假设分子为a,分母为b,虽然 a = a + b, 但此时a已经变成 a+b 了,所以再给b重 ...

  3. SQLServer数据库分页

    以  项目表 PM_Project  为例. PM_Project 全部内容如下(共6条数据): 一.Top – Not In - Top 方式分页 直接的,原始的,不采用函数,纯手动挡. 分步探索过 ...

  4. Velocity中为什么要使用{}来明确标识变量

    原因 比如在页面中,页面中有一个$someonename,此时,Velocity将把someonename作为变量名,若我们程序是想在someone这 个变量的后面紧接着显示name字符,则上面的标签 ...

  5. Notepad++的一个用法 转换为unix 格式的文件

    1. 跟昨天的linux 下面无法执行脚本的blog 一样 今天发现 notepad++ 有一个功能如下图: 双击 就能够选择文件的类型.. 转换为 unix 格式 就可以 在linux 下面执行了. ...

  6. socket基础编程-1

    server端和client端 1.server端: import socket server=socket.socket() server.bind(('localhost',8080)) serv ...

  7. python之路--模块和包

    一 . 模块 ⾸先,我们先看⼀个老⽣常谈的问题. 什么是模块. 模块就是⼀个包含了python定义和声明的⽂件, ⽂件名就是模块的名字加上.py后缀. 换句话说我们⽬前写的所有的py⽂件都可以看成是⼀ ...

  8. zsh & tree & macOS

    zsh & tree & macOS https://unix.stackexchange.com/questions/22803/counting-files-in-leaves-o ...

  9. python数据结构与算法第六天【栈与队列】

    1.栈和队列的原理 栈:后进先出(LIFO),可以使用顺序表和链表实现 队列:先进先出(FIFO),可以使用顺序表和链表实现 2.栈的实现(使用顺序表实现) #!/usr/bin/env python ...

  10. 老男孩python学习自修第十三天【md5加密】

    示例代码如下: hashlib_test.py #!/usr/bin/env python # _*_ coding:UTF-8 _*_ import hashlib def genPasswd(na ...