http://acm.fzu.edu.cn/problem.php?pid=1901

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70325#problem/Q

Period II

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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
 
给你一个字符串 s 求出所有满足s[i] == s[i+p] ( 0 < i+p < len )的 p ;

其实就是这个字符串的后缀与前缀的最大匹配 next[N],然后用最大匹配的串继续找匹配的前缀,比如下面的数据:
 
aaabaaa:--> P = 4  <---> next[7] = 3
aaabaaa:--> P = 5  <---> next[3] = 2
aaabaaa:--> P = 6  <---> next[2] = 1
aaabaaa:--> P = 7  <---> next[1] = 0
 
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm> using namespace std; const int maxn = ; char s[maxn];
int Next[maxn], ans[maxn]; void FindNext(char s[])
{
int slen = strlen(s), i=, j=-;
Next[] = -; while(i<slen)
{
if(j==- || s[i]==s[j])
Next[++i] = ++j;
else
j = Next[j];
}
} int main()
{
int t, iCase=;
scanf("%d", &t);
while(t--)
{
scanf("%s", s); int len = strlen(s); FindNext(s); int cnt = , k=len; while(Next[k]!=)
{
ans[cnt++] = len-Next[k]; /// 第 cnt 个子串结束的下标, 表示自己觉得很神奇
k = Next[k];
} printf("Case #%d: %d\n", iCase++, cnt+);
for(int i=; i<cnt; i++)
printf("%d ", ans[i]);
printf("%d\n", len);
}
return ;
}
 
 
 
 

(KMP Next的运用) Period II -- fzu -- 1901的更多相关文章

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

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

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

    题目大意:给你一个字符串 S ,N = |S|,如果存在一个 P (1<=P<=N),并且满足 s[i] = s[P+i] (i = {0...N-P-1} ),求出来所有的 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. FZU1901 Period II —— KMP next数组

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

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

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

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

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

  9. FZU 1901 Period II(KMP中的next)题解

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

随机推荐

  1. dmidecode详解

    1.DMI简介 DMI (Desktop Management Interface, DMI)就是帮助收集电脑系统信息的管理系统,DMI信息的收集必须在严格遵照SMBIOS规范的前提下进行. SMBI ...

  2. NodeJS框架express的路径映射(路由)功能及控制

    我 们知道Express是一个基于NodeJS的非常优秀的服务端开发框架,本篇CSSer将提供express框架的route和route control章节,route实现了客户端请求的URL的路径映 ...

  3. Python 遍历文件夹 listdir walk 的区别

    一.一级目录import os path = 'd:\file'; for filename in os.listdir(path): print(os.path.join(path,filename ...

  4. MySql LeftJoin On 与 Where的差异

    [MySql LeftJoin On 与 Where的差异] 存在两张表: 分别插入数据: 下面的语句一与语句二会产生不同的结果: 语句一: 结果: 语句二: 结果: 为什么会存在差异,这和on与wh ...

  5. Cause: org.xml.sax.SAXParseException; lineNumber: 45; columnNumber: 62; 元素内容必须由格式正确的字符数据或标记组成。

    三月 09, 2018 12:13:39 下午 org.apache.catalina.core.StandardContext listenerStart严重: Exception sending ...

  6. 关于插入date型数据

    create table student (name varchar2(10) not null primary key , enrolldate date not null);//创建student ...

  7. SpringMVC源码总结(一)HandlerMapping和HandlerAdapter入门

    SpringMVC在使用过程中,大多是使用注解,对它的实现接口之类的关系理解变得模糊, 通过对XML配置的理解,可以理清各个类的关系,譬如控制器类要实现Controller接口. 接触SpringMV ...

  8. #define宏常量和const常量的区别

    C++ 语言可以用const 来定义常量,也可以用#define 来定义常量.但是前者比后者有更多的优点:(1) const 常量有数据类型,而宏常量没有数据类型.编译器可以对前者进行类型安全检查.而 ...

  9. 使用Maven部署构件至私服

    --------------------siwuxie095                                 使用 Maven 部署构件至私服         1.部署构件到 Nexu ...

  10. Java 架构师

    “学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: 1.能出版出来的书一定是经过反复的思考.雕琢和审核的,因此从专业性的角度来说,一本好书的价值远超 ...