当前位置:网站首页>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;
}
边栏推荐
- Disable access to external entities in XML parsing
- Fabric.js 将本地图像上传到画布背景
- Principle and implementation of parallax effect
- LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
- kmp思想及模板代码
- 函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
- 在{{}}中拼接字符
- Splice characters in {{}}
- Video multiple effects production, fade in effect and border background are added at the same time
- Draw a wave chart_ Digital IC
猜你喜欢

Fabric. JS upload local image to canvas background

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

LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)

Fabric. JS iText superscript and subscript

LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
![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]

数据的储存

黑馬筆記---Set系列集合

No logic is executed after the El form is validated successfully

MySQL foundation --- query (learn MySQL foundation in 1 day)
随机推荐
Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
Fabric.js IText设置指定文字的颜色和背景色
Here comes the chicken soup! Keep this quick guide for data analysts
Pyechart1.19 national air quality exhibition
Database batch insert data
paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
Gee series: unit 8 time series analysis in Google Earth engine [time series]
Find the subscript with and as the target from the array
LeetCode 1175. 质数排列(质数判断+组合数学)
Disable access to external entities in XML parsing
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
从数组中找出和为目标的下标
7.TCP的十一种状态集
Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
Gee series: Unit 2 explore datasets
函数栈帧的创建和销毁
Fabric. JS activation input box
Gee series: unit 9 generate sampling data in GEE [random sampling]
画波形图_数字IC
fastText文本分类