当前位置:网站首页>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;
}
边栏推荐
- Johnson–Lindenstrauss Lemma(2)
- Cubemx DMA notes
- Fabric.js 精简JSON
- Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
- Find the subscript with and as the target from the array
- 函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
- Fabric.js 将本地图像上传到画布背景
- 创新永不止步——nVisual网络可视化平台针对Excel导入的创新历程
- Gee series: unit 7 remote sensing image classification using GEE [random forest classification]
- Set the default style of scroll bar Google browser
猜你喜欢
创新永不止步——nVisual网络可视化平台针对Excel导入的创新历程
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
操作符详解
Principle and implementation of parallax effect
Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import
Here comes the chicken soup! Keep this quick guide for data analysts
Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]
Fabric.js IText设置指定文字的颜色和背景色
Gee series: Unit 4 data import and export in Google Earth engine
Cubemx DMA notes
随机推荐
Operator details
Global and Chinese market of pressure gauges 2022-2028: Research Report on technology, participants, trends, market size and share
ubuntu20.04安装mysql8
Gee series: Unit 2 explore datasets
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
About PROFIBUS: communication backbone network of production plant
Principle and implementation of parallax effect
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Fabric. JS free draw rectangle
Record my pytorch installation process and errors
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
fastText文本分类
Paddlepaddle project source code
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
操作符详解
Detailed explanation of Pointer use
leetcode两数相加go实现
How to make an RPM file
CubeMx DMA笔记
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share