当前位置:网站首页>KMP idea and template code
KMP idea and template code
2022-07-02 05:20:00 【Where's huluwa's father】
Refer to the post :https://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html
video :https://www.acwing.com/video/259/
Example :https://www.acwing.com/problem/content/833/
BF Algorithm ( violence )
thought
BF Algorithm is violent solution , Double cycle , If there is a character mismatch, go back (i=i-j+1)
Code
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; // The pointer i to flash back
}
return -1;
}
KMP
thought
KMP The algorithm mainly solves optimization BF Algorithm ,BF When the fifth time in the algorithm does not match , The sixth time j Can be directly from 2 Start , No need to 0 Start , This redundant matching process occurs in kmp It's cancelled .kmp No more i=i-j+1 Pointer backtracking process of , Just think about it every time j Just a pointer 
next Array idea
Using the idea that the largest suffix is equal to the prefix
First of all, let's know what prefix is , What is a suffix .
With abacab For example
Prefix :a``ab``aba``abac``abaca
suffix :b``ab``cab``acab``bacab
It can be seen that the pre suffix does not include itself , among The largest suffix is equal to the prefix Means the longest equal prefix , In this case, yes ab
Code
#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;
// solve next Array
// because j=0 It's empty. ,j=1 It's the first number ,ne[1]=0, therefore j from 2 Start
for (int j = 2, k = 0; j <= n; j ++ )
{
// If it doesn't match, keep going backwards . There is no retreat (k!=0)
while (k!=0 && p[j] != p[k + 1]) k = ne[k];
if (p[j] == p[k + 1]) k ++ ;
ne[j] = k;
}
// matching
for (int i = 1, j = 0; i <= m; i ++ )
{
// If it doesn't match, keep going backwards . There is no retreat (j!=0)
while (j!=0 && s[i] != p[j + 1]) j = ne[j];
// If the match is successful, look at the next
if (s[i] == p[j + 1]) j ++ ;
if (j == n)
{
j = ne[j];
// The logic of a successful match
printf("%d ", i - n);
}
}
return 0;
}
边栏推荐
- Two implementation methods of delay queue
- 函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
- php/js cookie共享跨域的问题
- 国产全中文-自动化测试软件Apifox
- Cubemx DMA notes
- The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
- 【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
- Fabric. JS iText set italics manually
- Fabric. JS gradient
- 7.1模擬賽總結
猜你喜欢

Cubemx DMA notes

Nodejs (03) -- custom module

Fabric. JS upload local image to canvas background

Express logistics quick query method, set the unsigned doc No. to refresh and query automatically

paddle: ValueError:quality setting only supported for ‘jpeg‘ compression

CubeMx DMA笔记
![Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]](/img/1e/cf0aa09c2fce2278386f12eae4a6cd.jpg)
Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]

【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘

操作符详解

Fabric.js 居中元素
随机推荐
视差特效的原理和实现方法
Nodejs (03) -- custom module
Pyechats 1.19 generate a web version of Baidu map
Fabric.js 背景不受视口变换影响
Draw a wave chart_ Digital IC
go实现leetcode旋转数组
Feign realizes file uploading and downloading
Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
操作符详解
黑馬筆記---Set系列集合
Leetcode18题 【四数之和】递归解法
Foreign trade marketing website system development function case making
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Fabric. JS iText set italics manually
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
el form 表单validate成功后没有执行逻辑
Fabric.js 圆形笔刷
金融门户相关信息