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. 03、HelleBaiduMap

    D:\百度地图\百度地图\资料\百度地图与定位SDK\百度地图v3.5.0\BaiduMap_AndroidSDK_v3.5.0_All\BaiduMap_AndroidSDK_v3.5.0_Docs ...

  2. P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of  pipes to transport mil ...

  3. Java中多个线程交替循环执行

    有些时候面试官经常会问,两个线程怎么交替执行呀,如果是三个线程,又怎么交替执行呀,这种问题一般人还真不一定能回答上来.多线程这块如果理解的不好,学起来是很吃力的,更别说面试了.下面我们就来剖析一下怎么 ...

  4. String,StringBuffer和StringBuilder

    在执行速度方面的比较:StringBuilder > StringBuffer StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们对字符串做操 ...

  5. 题解报告:hdu 1285 确定比赛名次

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次 ...

  6. android GPS 定位,取位置信息

    现在很多app ,需要取位置信息,所以我也做了一个模块用来取位置信息:   加入位置服务所需的权限: <uses-permission android:name="android.pe ...

  7. Windows开发小问题集

    ON_BN_KILLFOCUS无效,因为需要BS_NOTIFY

  8. CNN结构:Windows使用FasterRCNN-C++版本

    参考文章:Windows下VS2013 C++编译测试faster-rcnn. 本文与作者的所写方法有些许不同,欲速则不达,没有按照作者的推荐方法,绕了个弯弯. Windows版本纯C++版本的Fas ...

  9. (转)Java任务调度框架Quartz入门教程指南(四)Quartz任务调度框架之触发器精讲SimpleTrigger和CronTrigger、最详细的Cron表达式范例

    http://blog.csdn.net/zixiao217/article/details/53075009 Quartz的主要接口类是Schedule.Job.Trigger,而触发器Trigge ...

  10. POJ_3279_(dfs)(状态)

    ---恢复内容开始--- Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8219   Accepted:  ...