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. APP被应用商店下架了怎么办?

    上周五的时候,知乎被各大APP应用商店下架,原因是因为在应用内一些信息不符合相关的规定所以被强制性下架,看起来只是简单的一个应用被下架的事情.但是作为一个推广人员,我第一件事情想到的是,假如我自己公司 ...

  2. jQuery查找子元素与后代元素

    1. 子元素: $().children('选择器')  如选择type为file的子元素  $(this).children("input[type=file]") 或者 $(& ...

  3. Java8中聚合操作collect、reduce方法详解

    Stream的基本概念 Stream和集合的区别: Stream不会自己存储元素.元素储存在底层集合或者根据需要产生.Stream操作符不会改变源对象.相反,它会返回一个持有结果的新的Stream.3 ...

  4. Python基础-多线程与多进程

    一,线程与进程之间的关系:(从知乎上看到的) 一个必须知道的事实:执行一段程序代码,实现一个功能的过程介绍 ,当得到CPU的时候,相关的资源必须也已经就位,就是显卡啊,GPS啊什么的必须就位,然后CP ...

  5. stl_iterator.h

    stl_iterator.h // Filename: stl_iterator.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...

  6. C语言小程序(七)、石头剪刀布

    本来挺简单的一个程序,但突然想把<Friends>给糅合进去,就多花了一些心思,这是我写过最有趣的程序了. #include <stdio.h> #include <st ...

  7. 9th

    2017-2018-2 20179212<网络攻防实践>第9周作业 视频学习 KaliSecurity压力测试工具 压力测试通过确定一个系统的瓶颈或者不能接受的性能点,来获得系统能够提供的 ...

  8. P1731 生日蛋糕

    题目背景 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层 生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1<=i<=M)层蛋糕是半径为Ri, 高度为Hi的圆柱 ...

  9. URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)

    解决:鼠标悬于上方Alt + Enter 选择Ignore

  10. BZOJ1217:[HNOI2003]消防局的设立

    我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...