当前位置:网站首页>kmp思想及模板代码
kmp思想及模板代码
2022-07-02 05:18:00 【葫芦娃的爸爸去哪了】
参考博文:https://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html
视频:https://www.acwing.com/video/259/
例题:https://www.acwing.com/problem/content/833/
BF算法(暴力)
思想
BF算法就是暴力解,双重循环,有字符不匹配就回溯(i=i-j+1)
代码
int BFMatch(char *s,char *p)
{
int i,j;
i=0;
while(i<strlen(s))
{
j=0;
while(s[i]==p[j]&&j<strlen(p))
{
i++;
j++;
}
if(j==strlen(p))
return i-strlen(p);
i=i-j+1; //指针i回溯
}
return -1;
}
KMP
思想
KMP算法解决的主要是优化BF算法,BF算法中第五趟不匹配时,第六趟j
可以直接从2开始,不需要从0开始,这种多余的匹配过程在kmp中取消了。kmp不再进行i=i-j+1
的指针回溯过程,只需要每次考虑j
指针即可
next数组思想
利用了最大的后缀等于前缀的思想
首先来了解一下什么是前缀,什么是后缀。
以abacab
为例
前缀:a``ab``aba``abac``abaca
后缀:b``ab``cab``acab``bacab
可以看出前后缀都不包括本身,其中最大的后缀等于前缀
的意思是最长的相等的前后缀,在这个例子中是ab
代码
#include <iostream>
using namespace std;
const int N = 100010, M = 1000010;
int n, m;
int ne[N];
char s[M], p[N];
int main()
{
cin >> n >> p + 1 >> m >> s + 1;
// 求解next数组
// 因为j=0是空的,j=1是第一个数,ne[1]=0,所以j从2开始
for (int j = 2, k = 0; j <= n; j ++ )
{
// 如果不匹配则一直往后退。退到退无可退(k!=0)
while (k!=0 && p[j] != p[k + 1]) k = ne[k];
if (p[j] == p[k + 1]) k ++ ;
ne[j] = k;
}
// 匹配
for (int i = 1, j = 0; i <= m; i ++ )
{
// 如果不匹配则一直往后退。退到退无可退(j!=0)
while (j!=0 && s[i] != p[j + 1]) j = ne[j];
// 如果匹配成功则看下一个
if (s[i] == p[j + 1]) j ++ ;
if (j == n)
{
j = ne[j];
// 匹配成功后的逻辑
printf("%d ", i - n);
}
}
return 0;
}
边栏推荐
- Black Horse Notes - - set Series Collection
- Fabric. JS free draw rectangle
- Gee: use of common mask functions in remote sensing image processing [updatemask]
- Gee dataset: chirps pentad high resolution global grid rainfall dataset
- Case sharing | intelligent Western Airport
- Gee series: Unit 2 explore datasets
- Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
- Fabric.js IText 上标和下标
- Feign realizes file uploading and downloading
- 7.1模拟赛总结
猜你喜欢
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
Video multiple effects production, fade in effect and border background are added at the same time
Paddlepaddle project source code
Ls1046nfs mount file system
Mysql基础---查询(1天学会mysql基础)
CubeMx DMA笔记
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Fabric.js 精简JSON
Creation and destruction of function stack frames
Fabric. JS upload local image to canvas background
随机推荐
删除排序数组中的重复项go语言实现
Gee: remote sensing image composite and mosaic
Gee series: unit 7 remote sensing image classification using GEE [random forest classification]
Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
函数栈帧的创建和销毁
Mathematical knowledge -- understanding and examples of fast power
Fabric.js IText 手动设置斜体
Splice characters in {{}}
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
CubeMx DMA笔记
About PROFIBUS: communication backbone network of production plant
C # picture display occupancy problem
Go implements leetcode rotation array
Johnson–Lindenstrauss Lemma(2)
Here comes the chicken soup! Keep this quick guide for data analysts
Global and Chinese markets of semiconductor laser therapeutics 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
go实现leetcode旋转数组
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
Using QA band and bit mask in Google Earth engine