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 ...
随机推荐
- .NET微信公众号开发-3.0查询自定义菜单
一.前言 前面我们已经创建好了我们的自定义菜单.那么我们现在要如何查询我们自定义的菜单.原理都是一样的,而且都是相当简单,只是接口地址文档换掉了. 2.0开始编码 同样我们首先创建好我的查询页面,在这 ...
- ASP.net绑定文本框Enter事件到按钮 ASP.NET执行后台执行JS方法
txtAccountBarcode.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if (( ...
- August 9th 2016, Week 33rd Tuesday
Tomorrow is never clear, our time is here. 明天是未知的,我们还是要过好当下. Tomorrow is not unpredictable, it is cl ...
- Java使用JDBC连接MySQL数据库
1.引用 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写 ...
- Tomcat在Linux上的安装与配置
以下使用的Linux版本为: Redhat Enterprise Linux 6.5 x86_64,Tomcat版本为tomcat-7.0.54. 1.下载JDK与Tomcat. jdk下载地址 ...
- Mac OS
defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.apple.finde ...
- linux cpuInfo
转自:http://blog.csdn.net/lgstudyvc/article/details/7889364 /proc/cpuinfo文件分析 在Linux系统中,提供了proc文件系统显 ...
- javascript 面向对象编程小记
虽然平常用jquery用的很熟,但是基本都是面向过程的写法.一个事件一个function,很少有面向对象的写法.今天得写一个日期控件,不得不用上面向对象编程. 刚开始我的想法是: var datepi ...
- Genymotion刷入谷歌应用市场以及获取root权限
Genymotion刷入谷歌应用市场以及获取root权限 - 推酷http://www.tuicool.com/articles/rEV3aa6 刷入gapp, arm框架,supersu的包要注意, ...
- Android OkHttp完全解析 --zz
参考文章 https://github.com/square/okhttp http://square.github.io/okhttp/ 泡网OkHttp使用教程 Android OkHttp完全解 ...