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. 在解析XML时要注意解析元素和解析标签属性的区别

    解析元素时: Sting str = ele.elementText("name"); 而解析标签属性时: String key = ele.attributeValue(&quo ...

  2. hbase_学习_00_资源帖

    一.官方资料 1.官网:http://hbase.apache.org/ 2.官方文档:HBase 官方文档中文版 二.apache软件下载基地 1. Apache Software Foundati ...

  3. Javascript-- jQuery事件篇(2)

    jQuery表单事件之blur与focus事件 单处理事件focusin事件与focusout事件,同样用于处理表单焦点的事件还有blur与focus事件 它们之间的本质区别: 是否支持冒泡处理 举个 ...

  4. java--xml文件读取(SAX)

    SAX解析原理: 使用Handler去逐个分析遇到的每一个节点 SAX方式解析步奏: 创建xml解析需要的handler(parser.parse(file,handler)) package com ...

  5. uoj279温暖会指引我们前行

    暖气来啦~ 动态树维护最大生成树裸题 #include<iostream> #include<cstdio> #include<cstdlib> #include& ...

  6. 学习大牛笔记nginx + gunicorn + supervisor

    安装 gunicorn pip install gunicorn pip 是一个重要的工具,python 用来管理包.还有一个最佳生产就是每次使用 pip 安装的库,都写入一个 requirement ...

  7. Oracle 12c 多租户在 CDB 中 Plug A PDB,Unplugging A PDB

    Oracle 数据库 12 c 中介绍了多租户选项允许单个容器数据库 (CDB) 来承载多个单独的可插拔数据库 (PDB).本文简单的演示了如何在 CDB 中 Plug A PDB,Unpluggin ...

  8. Oracle 12c 新特性之 temp undo

    Oracle 12c R1 之前,临时表生成的undo记录是存储在undo表空间里的,通用表和持久表的undo记录也是类似的.而在 12c R12 的临时 undo 功能中,临时 undo 记录可以存 ...

  9. 洛谷【P1480】A/B Problem

    题目传送门:https://www.luogu.org/problemnew/show/P1480 高精除低精板子题,灵性地回忆一下小学时期列竖式的草稿纸即可. 时间复杂度:\(O(len)\) 空间 ...

  10. 洛谷【P1601】A+B Problem(高精)

    题目传送门:https://www.luogu.org/problemnew/show/P1601 高精度加法板子.我们灵性地回忆一波小学学加法列竖式的场景(从\(6\)岁开始口算从未打过草稿的大佬请 ...