当前位置:网站首页>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;
}
边栏推荐
- Gee: use of common mask functions in remote sensing image processing [updatemask]
- Ubuntu 20.04 installing mysql8
- LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
- Fabric.js 3个api设置画布宽高
- Fabric.js 居中元素
- ERP management system development and design existing source code
- Here comes the chicken soup! Keep this quick guide for data analysts
- Case sharing | intelligent Western Airport
- Preparation for writing SAP ui5 applications using typescript
- Fabric.js 渐变
猜你喜欢
CubeMx DMA笔记
Mathematical knowledge -- understanding and examples of fast power
Fabric. JS upload local image to canvas background
Summary of database problems
Creation and destruction of function stack frames
Fabric.js IText设置指定文字的颜色和背景色
Gee: explore the change of water area in the North Canal basin over the past 30 years [year by year]
Nodejs (03) -- custom module
Gee: remote sensing image composite and mosaic
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
随机推荐
js中的Map(含leetcode例题)
Gee series: unit 9 generate sampling data in GEE [random sampling]
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
Mapping settings in elk (8) es
7.TCP的十一种状态集
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
Pyflink writes MySQL examples with JDBC
How to make an RPM file
[high speed bus] Introduction to jesd204b
[common error] the DDR type of FPGA device is selected incorrectly
数据库批量插入数据
设置滚动条默认样式 谷歌浏览器
指针使用详解
Mathematical knowledge -- understanding and examples of fast power
Case sharing | intelligent Western Airport
黑马笔记---Map集合体系
paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
Creation and destruction of function stack frames
Fabric.js 自由绘制矩形