当前位置:网站首页>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;
}
边栏推荐
- 4. Flask cooperates with a tag to link internal routes
- CubeMx DMA笔记
- Gee series: unit 6 building various remote sensing indexes in Google Earth engine
- centos8安装mysql8.0.22教程
- Nodejs (03) -- custom module
- Fabric.js IText 手动设置斜体
- 创新永不止步——nVisual网络可视化平台针对Excel导入的创新历程
- leetcode两数相加go实现
- 函数栈帧的创建和销毁
- Fabric. JS 3 APIs to set canvas width and height
猜你喜欢

Dark horse notes -- map set system

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

Fabric.js 居中元素

Operator details

6. Network - Foundation

Black Horse Notes - - set Series Collection

Fabric. JS upload local image to canvas background

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

视差特效的原理和实现方法

Preparation for writing SAP ui5 applications using typescript
随机推荐
画波形图_数字IC
Johnson–Lindenstrauss Lemma(2)
National all Chinese Automatic Test Software apifox
Operator details
[opencv] image binarization
Global and Chinese market of impact roll 2022-2028: Research Report on technology, participants, trends, market size and share
kmp思想及模板代码
7.1 Résumé du concours de simulation
leetcode两数相加go实现
Set the default style of scroll bar Google browser
6. Network - Foundation
【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
Fabric. JS compact JSON
摆正元素(带过渡动画)
Online English teaching app open source platform (customized)
國產全中文-自動化測試軟件Apifox
4. Flask cooperates with a tag to link internal routes
Detailed explanation of Pointer use
Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import