当前位置:网站首页>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;
}
边栏推荐
- Cubemx DMA notes
- Storage of data
- Gee series: Unit 1 Introduction to Google Earth engine
- Super detailed pycharm tutorial
- [quick view opencv] familiar with CV matrix operation with image splicing examples (3)
- Online English teaching app open source platform (customized)
- Domestic all Chinese automatic test software apifox
- Mysql基础---查询(1天学会mysql基础)
- Gee data set: export the distribution and installed capacity of hydropower stations in the country to CSV table
- National all Chinese Automatic Test Software apifox
猜你喜欢

Summary of database problems

Cubemx DMA notes

Detailed explanation of Pointer use

Black Horse Notes - - set Series Collection

操作符详解
![Gee series: unit 8 time series analysis in Google Earth engine [time series]](/img/a6/648ff959af93c22dc8605215a90535.jpg)
Gee series: unit 8 time series analysis in Google Earth engine [time series]

The El cascader echo only selects the questions that are not displayed

Collectors.groupingBy 排序

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

运维工作的“本手、妙手、俗手”
随机推荐
Implementation of go language for deleting duplicate items in sorting array
Fabric.js 右键菜单
删除排序数组中的重复项go语言实现
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Global and Chinese market of impact roll 2022-2028: Research Report on technology, participants, trends, market size and share
paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
案例分享|智慧化的西部机场
Disable access to external entities in XML parsing
Using QA band and bit mask in Google Earth engine
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
Find the subscript with and as the target from the array
Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
Gee dataset: chirps pentad high resolution global grid rainfall dataset
Leetcode 18 problem [sum of four numbers] recursive solution
6. Network - Foundation
Essence and physical meaning of convolution (deep and brief understanding)
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
运维工作的“本手、妙手、俗手”
【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
6.网络-基础