当前位置:网站首页>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;
}
边栏推荐
- Oracle和MySQL的基本区别(入门级)
- No logic is executed after the El form is validated successfully
- Splice characters in {{}}
- Find the subscript with and as the target from the array
- 6.网络-基础
- Exercise notes 13 (effective letter ectopic words)
- 设置滚动条默认样式 谷歌浏览器
- 摆正元素(带过渡动画)
- 國產全中文-自動化測試軟件Apifox
- Using QA band and bit mask in Google Earth engine
猜你喜欢

Storage of data

el form 表单validate成功后没有执行逻辑

Fabric.js 将本地图像上传到画布背景

LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)

【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘

Cubemx DMA notes

About PROFIBUS: communication backbone network of production plant

Dark horse notes -- Set Series Collection

el-cascader回显只选中不显示的问题

Nodejs (03) -- custom module
随机推荐
Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
延时队列两种实现方式
【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
数据的储存
leetcode两数相加go实现
Fabric.js 精简JSON
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
7.1模拟赛总结
Gee series: Unit 2 explore datasets
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
Fabric.js 激活输入框
Set the default style of scroll bar Google browser
Fabric.js 右键菜单
Map in JS (including leetcode examples)
Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
黑馬筆記---Set系列集合
Super detailed pycharm tutorial
Summary of MySQL key challenges (2)
Gee series: Unit 1 Introduction to Google Earth engine
Leetcode basic programming: array