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 一开始写的时候不小心把求ans数组写成了递归,导致overflow。
#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXN 1001000
/*
寻找字符串中所有 前缀 和(后缀逆转) 的匹配
将字符串逆转后 添加到原字符串末尾
然后输出所有小于源字符串长度的解
*/
char s[MAXN];
int Next[MAXN],l,cnt;
int ans[MAXN];
void kmp_pre(int m)
{
int j,k;
j = ; k = Next[] = -;
while(j<m)
{
if(k==-||s[j]==s[k])
Next[++j] = ++k;
else
k = Next[k];
}
}
void Cnt(int p)
{
while(Next[p]>=)
{
ans[cnt++] = l-Next[p];
p = Next[p];
}
}
int main()
{
int T;
scanf("%d",&T);
for(int i=;i<=T;i++)
{
cnt = ;
scanf("%s",s);
l = strlen(s);
kmp_pre(l);
Cnt(l);
printf("Case #%d: %d\n",i,cnt);
for(int j=;j<cnt;j++)
{
if(j) printf(" ");
printf("%d",ans[j]);
}
cout<<endl;
}
}

Q - Period II的更多相关文章

  1. (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 ...

  2. FZU1901 Period II —— KMP next数组

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

  3. Fzu Problem 1901 Period II (kmp)

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

  4. 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 ...

  5. [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 ...

  6. Period II

    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 ...

  7. FZU - 1901 Period II (kmp)

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

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

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

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

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

随机推荐

  1. 等价表达式 2005年NOIP全国联赛提高组(栈模拟)

    P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的 ...

  2. 微信小程序后台获取用户的opeid

    1.微信小程序后台获取登录用户的openid,首先微信小程序将code传给后台服务器 wx.login({ success: function (res) { var code = res.code ...

  3. Map,Filter 和 Reduce

    Map会将一个函数映射到一个输入列表的所有元素上 map(function_to_apply, list_of_inputs) items = [1, 2, 3, 4, 5] squared = li ...

  4. ACM_Alien And Password

    Alien And Password Time Limit: 2000/1000ms (Java/Others) Problem Description: Alien Fred wants to de ...

  5. Hadoop Hive概念学习系列之hive里的扩展接口(CLI、Beeline、JDBC)(十六)

    <Spark最佳实战  陈欢>写的这本书,关于此知识点,非常好,在94页. hive里的扩展接口,主要包括CLI(控制命令行接口).Beeline和JDBC等方式访问Hive. CLI和B ...

  6. 生成错误:对路径".dll"的访问被拒绝

    第一步:检查dll所在的目录的访问权限,右键文件夹>属性>安全>设置添加EveryOne用户并将完全控制的权限赋给它. 如果问题还没有解决,请不要一遍遍的重启,看第二步: 第二步:右 ...

  7. SAS进阶《深入解析SAS》之SAS数据挖掘的一般流程

    SAS进阶<深入解析SAS>之SAS数据挖掘的一般流程 1. 所谓数据挖掘,是指通过对大量的数据进行选择.探索与建模,来揭示包含在数据中以前不为人所知的模式或规律,从而为商业活动或科学研究 ...

  8. DeadObjectException

    开发的过程中有时候会遇到DeadObjectException,说明系统service已经停止运行,解决的方式是在mainfistxml的application标签中添加android:hardwar ...

  9. Unity 引擎UGUI之自定义树形菜单(TreeView)

    先上几张效果图:          如果你需要的也是这种效果,那你就来对地方了! 目前,我们这个树形菜单展现出来的功能如下: 1.可以动态配置数据源: 2.点击每个元素的上下文菜单按钮(也就是图中的三 ...

  10. 【译】x86程序员手册07 - 2.5操作数选择

    2.5 Operand Selection 操作数选择 An instruction can act on zero or more operands, which are the data mani ...