当前位置:网站首页>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;
}
边栏推荐
- ERP management system development and design existing source code
- Leetcode18题 【四数之和】递归解法
- Global and Chinese markets of semiconductor laser therapeutics 2022-2028: Research Report on technology, participants, trends, market size and share
- Find the subscript with and as the target from the array
- Pyechats 1.19 generate a web version of Baidu map
- Gee series: Unit 1 Introduction to Google Earth engine
- Go implements leetcode rotation array
- Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
- About PROFIBUS: communication backbone network of production plant
- There are duplicate elements in leetcode. Go implementation
猜你喜欢

Fabric.js 激活输入框

Fabric.js 渐变

Video multiple effects production, fade in effect and border background are added at the same time

Preparation for writing SAP ui5 applications using typescript

Simple and practical accounting software, so that accounts can be checked

No logic is executed after the El form is validated successfully

Fabric.js 右键菜单

黑马笔记---Map集合体系

Straighten elements (with transition animation)

Record my pytorch installation process and errors
随机推荐
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
About PROFIBUS: communication backbone network of production plant
6. Network - Foundation
Fabric. JS compact JSON
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
Fabric. JS right click menu
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
4. Flask cooperates with a tag to link internal routes
centos8安装mysql8.0.22教程
Fabric.js 右键菜单
Foreign trade marketing website system development function case making
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
Gee series: unit 8 time series analysis in Google Earth engine [time series]
Paddlepaddle project source code
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
Gee: create a new feature and set corresponding attributes
Php/js cookie sharing across domains
Pyflink writes MySQL examples with JDBC
函数栈帧的创建和销毁
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas