[FZU 1901]Period II KMP
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
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
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的更多相关文章
- 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 ...
- FZU 1901 Period II(KMP循环节+公共前后缀)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 题目大意:题目大意求出所有p满足s[i]=s[i+p](i<=len-p) 解题思路: 其实就是要 ...
- Fzu Problem 1901 Period II (kmp)
题目链接: Problem 1901 Period II 题目描述: 给出一个串,满足长度为p的前缀和长度为p的后缀相等的p的个数,输出p的个数,和p分别是多少? 解题思路: 对kmp的next数组的 ...
- FZU - 1901 Period II (kmp)
传送门:FZU - 1901 题意:给你个字符串,让你求有多少个p可以使S[i]==S[i+P] (0<=i<len-p-1). 题解:这个题是真的坑,一开始怎么都觉得自己不可能错,然后看 ...
- FZU 1901 Period II(KMP中的next)题解
题意:给你一串字符串,问你前后缀相同情况有几种,并输出后缀位置(?这里一直没看懂length是什么,但是这样理解答案也对,然后还要加上本身长度) 思路:这里好好讲讲next的用法.我们都知道next代 ...
- FZU1901 Period II —— KMP next数组
题目链接:https://vjudge.net/problem/FZU-1901 Problem 1901 Period II Accept: 575 Submit: 1495Time Lim ...
- Period II FZU - 1901(拓展kmp)
拓展kmp板题 emm...我比较懒 最后一个字母进了vector两个1 不想改了...就加了个去重... 哈哈 #include <iostream> #include <cst ...
- (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 ...
- Period II - FZU 1901(KMP->next)
题目大意:给你一个字符串 S ,N = |S|,如果存在一个 P (1<=P<=N),并且满足 s[i] = s[P+i] (i = {0...N-P-1} ),求出来所有的 P 然后输出 ...
随机推荐
- Netty组件理解(转载)
转载的文章,写的非常好. 一.先纵览一下Netty,看看Netty都有哪些组件? 为了更好的理解和进一步深入Netty,我们先总体认识一下Netty用到的组件及它们在整个Netty架 ...
- codeforces 553A A. Kyoya and Colored Balls(组合数学+dp)
题目链接: A. Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 属性成员是isXxx时对应的get方式是isXxx,前台jsp取不到这个属性值
最近在项目中无意设置的boolean变量值为isXxx,用eclipse生成相应的set和get方法,eclipse生成的的boolean类型的get方法为isXxx,前台导致取不到相应的值 publ ...
- 原来问题在这里-我的memory leak诊断历程
自从公司开始将java作为主要开发语言后,C++与java的混合应用日趋增多. java与C++的通信主要也是使用JNI来完成,这并没有什么问题.对于这样的混合应用项目来说,最大的噩梦莫过于memor ...
- 【转】mysql查询当天所有数据sql语句
mysql查询当天的所有信息: select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) a ...
- dubbo、dubbox、motan、thrift、grpc等RPC框架比较及选型
概述 前段时间项目要做服务化,所以我比较了现在流行的几大RPC框架的优缺点以及使用场景,最终结合本身项目的实际情况选择了使用dubbox作为rpc基础服务框架.下面就简单介绍一下RPC框架技术选型的过 ...
- 报错apachectl -t
httpd: apr_sockaddr_info_get() failed for wwwhttpd: Could not reliably determine the server's fully ...
- linux命令-rpm查询包
安装了哪些rpm包呢 [root@wangshaojun Packages]# rpm -qa /////查看全部安装的包 [root@wangshaojun Packages]# rpm -qa l ...
- url传参解决中文乱码
跳转前: window.open("http://localhost:9728/content/agent/devolution.html?search_agent=" + enc ...
- fabric差异化部署mysql和lnmp
1.代码如下: vim lnmp.py ------------------------------------------> #!/usr/bin/env python from fabric ...