当前位置:网站首页>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;
}
边栏推荐
- 设置滚动条默认样式 谷歌浏览器
- Set the default style of scroll bar Google browser
- 2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
- Leetcode18题 【四数之和】递归解法
- Summary of MySQL key challenges (2)
- JS interview collection test question 1
- Fabric. JS upload local image to canvas background
- Video cover image setting, put cover images into multiple videos in the simplest way
- go实现leetcode旋转数组
- Operator details
猜你喜欢

No logic is executed after the El form is validated successfully

Gee series: unit 6 building various remote sensing indexes in Google Earth engine

Gee series: Unit 2 explore datasets

Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers

操作符详解

Summary of database problems

Gee: create a new feature and set corresponding attributes

Gee series: Unit 4 data import and export in Google Earth engine

Collectors.groupingBy 排序

【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
随机推荐
How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure
Dark horse notes -- map set system
[high speed bus] Introduction to jesd204b
Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
Fabric. JS free draw rectangle
CubeMx DMA笔记
Splice characters in {{}}
Foreign trade marketing website system development function case making
数据库批量插入数据
C # picture display occupancy problem
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
leetcode两数相加go实现
Gee: remote sensing image composite and mosaic
C case of communication between server and client based on mqttnet
Fabric. JS right click menu
ubuntu20.04安装mysql8
js中的Map(含leetcode例题)
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
Record my pytorch installation process and errors