KMP(fail数组应用) LA 3026 Period
题意:(训练指南P213) 求每个前缀的最短循环节
分析:利用失配函数的性质,如果i % (i - fail[i]) == 0,那么正好错位移动一个循环节长度。
#include <bits/stdc++.h>
using namespace std; const int N = 1e6 + 5;
char str[N];
int fail[N];
int len; void get_fail(char *P) {
int i = 0, j = -1; fail[i] = j;
while (i < len) {
if (j == -1 || P[j] == P[i]) {
fail[++i] = ++j;
}
else j = fail[j];
}
} int main(void) {
int cas = 0;
while (scanf ("%d", &len) == 1) {
if (!len) break;
scanf ("%s", &str);
get_fail (str);
for (int i=0; i<=len; ++i) {
printf ("%d ", fail[i]);
} puts ("");
printf ("Test case #%d\n", ++cas);
for (int i=1; i<=len; ++i) {
if (fail[i] >= 0 && i % (i - fail[i]) == 0) {
int k = i / (i - fail[i]);
if (k > 1) {
printf ("%d %d\n", i, k);
}
}
}
puts ("");
} return 0;
}
KMP(fail数组应用) LA 3026 Period的更多相关文章
- LA 3026 - Period KMP
看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- LA 3026 Period
这只是蓝书上的一道KMP水题...然后对于最长前缀的循环证明我就不说了... #include<iostream> #include<cstring> #include< ...
- HDU 1358 Period(KMP next数组运用)
Period Problem Description For each prefix of a given string S with N characters (each character has ...
- 【暑假】[实用数据结构]UVAlive 3026 Period
UVAlive 3026 Period 题目: Period Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- [BZOJ 1535] [Luogu 3426]SZA-Template (KMP+fail树+双向链表)
[BZOJ 1535] [Luogu 3426]SZA-Template (KMP+fail树+双向链表) 题面 Byteasar 想在墙上涂一段很长的字符,他为了做这件事从字符的前面一段中截取了一段 ...
- UVALive - 3026 Period kmp next数组的应用
input n 2<=n<=1000000 长度为n的字符串,只含小写字母 output Test case #cas 长度为i时的最小循环串 循环次数(>1) 若没有则不输出 做法 ...
- LA 3026 && POJ 1961 Period (KMP算法)
题意:给定一个长度为n字符串s,求它每个前缀的最短循环节.也就是对于每个i(2<=i<=n),求一个最大整数k>1(如果存在),使得s的前i个字符组成的前缀是某个字符串重复得k次得到 ...
- UVALive 3026 Period (KMP算法简介)
kmp的代码很短,但是不太容易理解,还是先说明一下这个算法过程吧. 朴素的字符串匹配大家都懂,但是效率不高,原因在哪里? 匹配过程没有充分利用已经匹配好的模版的信息,比如说, i是文本串当前字符的下标 ...
- FZU1901 Period II —— KMP next数组
题目链接:https://vjudge.net/problem/FZU-1901 Problem 1901 Period II Accept: 575 Submit: 1495Time Lim ...
随机推荐
- OSG osgDB FileUtils FileNameUtil操作文件名相关函数
/** Gets the parent path from full name (Ex: /a/b/c.Ext => /a/b). */extern OSGDB_EXPORT std::stri ...
- 模拟赛1102d1
炮(cannon)[题目描述]众所周知,双炮叠叠将是中国象棋中很厉害的一招必杀技.炮吃子时必须隔一个棋子跳吃,即俗称"炮打隔子". 炮跟炮显然不能在一起打起来,于是rly一天借来了 ...
- 模拟赛1031d1
NP(np)Time Limit:1000ms Memory Limit:64MB题目描述LYK 喜欢研究一些比较困难的问题,比如 np 问题.这次它又遇到一个棘手的 np 问题.问题是这个样子的:有 ...
- 选择题(codevs 2919)
2919 选择题 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 某同学考试,在N*M的答题卡上写 ...
- 苹果应用 Windows 申请 普通证书 和Push 证书 Hbuilder 个推(2)
s上一篇 讲述了android 如何打包,这一篇 看一下如何IOS下打包 在苹果上申请证书,及其麻烦,我写下来,有需要的直接拿走即可: 首先 苹果的证书分两种 一种是 development 证书,另 ...
- MVC – 4.mvc初体验(2)
5.显示学员列表 效果 数据表 5.1 首先,在文件夹Models新建一个新建项(W),选择ADO.NET 实体数据模型 (SingleTest.edmx) 5.2 建一个控制器,StudentsCo ...
- 南阳理工 题目9:posters(离散化+线段树)
posters 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that ...
- JavaScript获取当前根目录
JavaScript获取当前根目录 主要用到Location 对象,包含有关当前 URL 的信息,是 Window 对象的一个部分,可通过 window.location 属性来访问. 方法一 (wi ...
- 人人网的账号登录及api操作
.renren.php <?php /** * PHP Library for renren.com * * @author */ class renrenPHP { function __co ...
- hdu 4738 2013杭州赛区网络赛 桥+重边+连通判断 ***
题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥,使得这n座岛不连通,求最少要派多少人去. 处理重边 边在遍历的时候,第一个返回的一定是之前去的边,所以 ...