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 求字符串所有的自匹配的前缀和后缀,Next数组可以递归的求该字符串每个匹配的前缀和后缀的长度
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = +;
char s[maxn];
int Next[maxn];
int ans[maxn]; void getNext()
{
int len = strlen(s);
int j = ;
int t = Next[] = -;
while (j < len) {
if (t == - || s[j] == s[t])
Next[++j] = ++t;
else
t = Next[t];
}
} int main()
{
//freopen("1.txt", "r", stdin);
int T;
scanf("%d", &T);
for (int t = ; t <= T; t++) {
scanf("%s", s);
int cnt = ;
int len = strlen(s);
getNext();
int i = Next[len];
ans[cnt++] = len-i;
while (Next[i] >= ) {
i = Next[i];
ans[cnt++] = len-i;
}
printf("Case #%d: %d\n", t, cnt);
for (int i = ; i < cnt-; i++)
printf("%d ", ans[i]);
printf("%d", ans[cnt-]);
printf("\n");
} return ;
}
 

[FZU 1901]Period II KMP的更多相关文章

  1. FZU - 1901 Period II(kmp所有循环节)

    Problem Description For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SI ...

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

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

  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. FZU 1901 Period II(KMP中的next)题解

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

  6. FZU1901 Period II —— KMP next数组

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

  7. Period II FZU - 1901(拓展kmp)

    拓展kmp板题 emm...我比较懒 最后一个字母进了vector两个1  不想改了...就加了个去重... 哈哈 #include <iostream> #include <cst ...

  8. (KMP Next的运用) Period II -- fzu -- 1901

    http://acm.fzu.edu.cn/problem.php?pid=1901 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=703 ...

  9. Period II - FZU 1901(KMP->next)

    题目大意:给你一个字符串 S ,N = |S|,如果存在一个 P (1<=P<=N),并且满足 s[i] = s[P+i] (i = {0...N-P-1} ),求出来所有的 P 然后输出 ...

随机推荐

  1. 修改(python) mysql 数据库 使其可以支持插入中文

    先建立一个数据库,名字叫xsk #!/usr/bin/python3 # -*- coding:utf-8 -*- import pymsql # 打开数据库连接(本机,用户,密码,数据库名) db ...

  2. codeforces 653C C. Bear and Up-Down(乱搞题)

    题目链接: C. Bear and Up-Down time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  3. 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个升序的数组,把他转换成一个 ...

  4. 扩展欧几里得算法(exgcd)

    Bezout定理: 对于任意整数a,b,存在一对整数x,y满足:a*x+b*y=gcd(a,b) 证明如下: 在欧几里得算法的最后一步:b=0,即:gcd(a,0)=a 对于b>0,根据欧几里得 ...

  5. bzoj 4766: 文艺计算姬 矩阵树定理

    题目: 给定一个一边点数为\(n\),另一边点数为\(m\),共有\(n*m\)条边的带标号完全二分图\(K_{n,m}\) 计算其生成树个数 \(n,m,p \leq 10^{18} ,p为模数\) ...

  6. python日志轮转RotatingFileHandler在django中的一个bug

    简介 大量过时的日志会占用硬盘空间,甚至长时间运行不注意会占满硬盘导致宕机,那么就可以使用内建logging模块根据文件大小(logging.handlers.RotatingFileHandler) ...

  7. 系列文章--Node.js学习笔记系列

    Node.js学习笔记系列总索引 Nodejs学习笔记(一)--- 简介及安装Node.js开发环境 Nodejs学习笔记(二)--- 事件模块 Nodejs学习笔记(三)--- 模块 Nodejs学 ...

  8. java ----一个函数传回多个值的总结

    java 一个函数如何返回多个值 参考方法: 1.使用map返回值:这个方法问题是,你并不知道如何返回值的key是什么,只能通过doc或者通过源代码来查看. 2.传入一个引用进去,修改引用的属性值.问 ...

  9. 插曲一--记《数据结构与问题求解(Java语言版)(第4版)》翻译问题

    在该书的527页中18.6理论题中,书中这样写道"完全结点是指每个结点都有两个孩子.证明,完全二叉树的结点数加1等于叶子树." 初看此题目,本人觉得很纳闷,再细细想之,发现似乎是个 ...

  10. 转载:IntelliJ IDEA 2016.2 配置Tomcat 运行Web项目

    以前都用MyEclipse写程序的 突然用了IDEA各种不习惯的说 借鉴了很多网上好的配置办法,感谢各位大神~ 前期准备 IDEA.JDK.Tomcat请先在自己电脑上装好 好么~ 博客图片为主 请多 ...