kmp

KMP字符匹配算法

冷暖自知 提交于 2019-11-30 23:56:52
KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt提出的,因此人们称它为克努特—莫里斯—普拉特操作(简称KMP算法)。KMP算法的核心是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的。具体实现就是通过一个next()函数实现,函数本身包含了模式串的局部匹配信息。KMP算法的时间复杂度O(m+n)。 下面是一个c++语言的简单实现 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; int Snext(string m,int next[]){ int i,j,l; i=0; j=-1; next[0]=-1; l=m.length(); while(i<l){ if(j == -1 || m[i]==m[j]){ i++;j++;next[i]=j; //cout<<i<<" "<<j<<" "<<next[i]<<endl; } else j=next[j]; } } int KMP(string s,string m,int next[]){ int i,j,lm,ls; i=j=0; lm=m.length(); ls=s.length(); while(i<ls&&j<lm){ if(j==-1||s

2019CCPC秦皇岛 J MUV LUV EXTRA(KMP)

佐手、 提交于 2019-11-30 16:42:00
MUV LUV EXTRA Time Limit: 2000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 134 Accepted Submission(s): 49 Problem Description One day, Kagami Sumika is stuck in a math problem aiming at calculating the length of a line segment with given statements and constraints. Since Sumika has no idea about it, she takes out a ruler and starts to measure the length. Unfortunately, the answer is an infinite decimal and she only got the first some digits of the answer from the ruler. Sumika guesses that the answer is a rational number, which means that there exists

KMP自动机

♀尐吖头ヾ 提交于 2019-11-30 16:18:39
KMP自动机 分类:字符串 内容:详细版 前置知识 不会的可以点击链接(如果有)或者前往 OI-Wiki 学习 KMP 一些约定 字符集大小默认为 m 模板字符串默认为 s 文本字符串默认为 t |s| 指字符串 s 的长度 字符串下标默认从 1 开始 简介 KMP自动机主要用于字符串的匹配问题,预处理复杂度为 O(|s|*m) ,可以以严格 O(|t|) 的复杂度进行字符串匹配( KMP 为均摊 O(|t|) ) ,并且可以处理可持久化字符串匹配问题 。 同时 KMP 自动机也是 AC 自动机(可以处理多个模板串的匹配)的基础。 构造 KMP 自动机 KMP 自动机与 KMP 的区别在于 KMP 自动机额外求出了 \(trans_{i,j}\) 表示在第 i 位置上往后匹配一个 j 字符会转移到什么状态(状态在这里指已经成功匹配了多少个字符)。 在下文中,将用 fail 来代替 KMP 的 next , nxt 代替上面的 trans。 普通地实现 KMP 自动机 假设我们处理到了第 i 个状态并且前 i-1 个状态已经完全处理好了,当前的 fail 也指向了正确的位置。 考虑每个 nxt 指向的状态: nxt[s[i + 1]] 显然指向 i+1 。 其余的 nxt 应当指向一直跳 fail 后第一个下一个字符能匹配的位置,即: nxt[i][j] = i; while(nxt

数据结构——KMP(串)

喜夏-厌秋 提交于 2019-11-30 13:31:48
KMP一个非常经典的字符串模式匹配算法   先来说说 KMP 的历史吧。 一、背景      KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt提出的,因此人们称它为克努特—莫里斯—普拉特操作(简称KMP算法)。KMP算法的核心是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的。具体实现就是通过一个next()函数实现,函数本身包含了模式串的局部匹配信息。KMP算法的时间复杂度度O(m+n)。KMP也可以处理最重复长子串问题,最长子串问题……这里挂一道最简单的题leetcode的 实现 strStr() ,大家看完可以去试一试。 补充说明:    强调两个概念: 真前缀 ,真后缀     如图所示,所谓的真前缀,就是在指在除了自身之外的全部字符串的头部顺序组合;而"真后最",就是指在除自身之外的一个字符串的全部尾串的顺序组合 。与后缀、前缀不同:              真前/后缀不包含自身字符!!! 二、朴素字符串匹配算法   其实就是我们最开始的时候写的字符串匹配,就是两个字符串逐一匹配。不作详细介绍,代码如下 /** * @brief 朴素字符串匹配 * @note * @param MainString: 主串 * @param Pattern: 模式串 * @retval */ int

HDU-4300-Clairewd's message(扩展KMP)

旧巷老猫 提交于 2019-11-30 13:31:43
链接: https://vjudge.net/problem/HDU-4300 题意: Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important messages and she was preparing for sending it to ykwd. They had agreed that each letter of these messages would be transfered to another one according to a conversion table. Unfortunately, GFW(someone's name, not what you just think about) has detected their action. He also got their conversion table by some unknown methods before. Clairewd was so clever and vigilant that when she realized that somebody was monitoring their action, she just stopped

kmp dp hdu 3336

匆匆过客 提交于 2019-11-30 13:13:37
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17918 Accepted Submission(s): 8106 http://acm.hdu.edu.cn/showproblem.php?pid=3336 Problem Description It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: s: "abab" The prefixes are: "a", "ab", "aba", "abab" For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba"

kmp hdu 2594

那年仲夏 提交于 2019-11-30 12:51:13
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 19249 Accepted Submission(s): 6521 http://acm.hdu.edu.cn/showproblem.php?pid=2594 Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marge: Yeah, what is it? Homer: Take me for example. I want to find out if I have a talent in politics, OK? Marge: OK. Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix in Clinton’s name that is a suffix in my name. That’s

洛谷-p5410-扩展KMP模板

三世轮回 提交于 2019-11-30 12:35:48
链接: https://www.luogu.org/problem/P5410#submit 题意: 有两个字符串aa,bb,要求输出bb与aa的每一个后缀的最长公共前缀 思路: 扩展kmp模板, 上一个大佬的详解链接 https://segmentfault.com/a/1190000008663857 代码: #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5+10; char a[MAXN], b[MAXN]; int Next[MAXN], Exten[MAXN]; void GetNext(char *t) { int p = 0, a = 0; int len = strlen(t); Next[0] = len; for (int i = 1;i < len;i++) { if (i >= p || i+Next[i-a] >= p) { if (i >= p) p = i; while (p < len && t[p] == t[p-i]) p++; Next[i] = p-i; a = i; } else Next[i] = Next[i-a]; } } void ExKmp(char *s, char *t) { int a = 0, p = 0; int len = strlen

HDU-1358-Period(KMP, 循环节)

早过忘川 提交于 2019-11-30 10:34:46
链接: https://vjudge.net/problem/HDU-1358#author=0 题意: For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A K , that is A concatenated K times, for some string A. Of course, we also want to know the period K. 思路: 先求出Next数组, 然后遍历一遍, 求出每个前缀的循环节, 再判断是否满足即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector

HDU-3746-Cyclic nacklace(KMP, 循环节)

℡╲_俬逩灬. 提交于 2019-11-30 10:26:14
链接: https://vjudge.net/problem/HDU-3746 题意: 第一题来啦。 现在给你一个字符串,请问在该字符串末尾最少添加多少个字符,可以让这个字符串获得重复循环序列。 思路: 考虑一个用字符串长度为len, 并且是由长度为l的子串循环组成. 我们有S[0,len-l-1] = S[l, len], 在循环次数大于2的时候, 所以len - Next[len] 就是最小循环节的长度. 画画图就好懂了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> //#include <memory.h> #include <queue> #include <set> #include <map> #include <algorithm> #include <math.h> #include <stack> #include <string> #include <assert.h> #include <iomanip> #include <iostream> #include <sstream> #define MINF 0x3f3f3f3f using namespace std; typedef long long LL; const int MAXN =