当前位置:网站首页>KMP template - string matching
KMP template - string matching
2022-07-27 17:19:00 【Folded cookies】
Here's the catalog title
1. subject

Text string :haystack
Pattern string ( Substring ):needle
The meaning of the question is to judge whether the text string contains a pattern string , If yes, the first matching position is returned
2. solution
2.1 Violence :
Double pointer , A point to the beginning of the text string , Start of a pointing pattern string
Time complexity :O(mn)
class Solution {
public:
int strStr(string haystack, string needle) {
int len1=haystack.size(),len2=needle.size();
if(len1==0&&len2==0)return 0;
for(int i=0;i<len1;i++){
bool flag=true;
for(int j=0;j<len2;j++){
if(i+j>=len1||haystack[i+j]!=needle[j]){
flag=false;
break;
}
}
if(flag)return i;
}
return -1;
}
};
2.2 KMP Algorithm :
thought : When there is a string mismatch , You can know a part of the text that has been matched before , You can use this information to avoid matching from scratch
next Array : It's quite a prefix table , When the record text string does not match the pattern string , Where should the pattern string start matching . That is, when the current position matching fails , Find the matching position in the previous pattern string , And then rematch .
prefixes : Record subscripts i Before ( Include i) In the string of , What is the length of the same Prefix suffix
eg. character string a The prefix of the longest face of is 0, character string aa The prefix of the longest face of is 1, character string aaa The prefix of the longest face of is 2
Prefix : All consecutive substrings starting with the first character that do not contain the last character
suffix : All consecutive substrings that do not contain the first character and end with the last character
Why use prefix table ?

When index=5 when , Pattern string (0,4] The longest equal prefix and suffix strings in the string are substrings aa, When index=5 If it does not match , Then jump to index=2 Continue to match where
Time complexity :O(n+m)
Be careful next Arrays may have been used in some header file , There may be a mistake , It is suggested to call it ne
Templates
// s[] Is a text string ,p[] It's a pattern string ,n yes s The length of ,m yes p The length of
// Find the pattern string Next Array :
s=" "+s;//char s[N];cin>>s+1;
p=" "+p;//char p[N];cin>>p+1;
// Only from 2 Start , from 1 It matched at the beginning
for (int i = 2, j = 0; i <= m; i ++ )
{
// When j There is no return to the starting point
while (j && p[i] != p[j + 1]) j = ne[j];
if (p[i] == p[j + 1]) j ++ ;
ne[i] = j;
}
// matching
for (int i = 1, j = 0; i <= n; i ++ )
{
while (j && s[i] != p[j + 1]) j = ne[j];
if (s[i] == p[j + 1]) j ++ ;
// The match is successful
if (j == m)
{
j = ne[j];
// The logic of a successful match
}
}
Answer key
class Solution {
public:
int strStr(string haystack, string needle) {
int len1=haystack.size(),len2=needle.size();
if(len1==0&&len2==0)return 0;
vector<int>ne(len2+1,0);
haystack=" "+haystack;
needle=" "+needle;
for(int i=2,j=0;i<=len2;i++){
while(j&&needle[i]!=needle[j+1])j=ne[j];
if(needle[i]==needle[j+1])j++;
ne[i]=j;
}
for(int i=1,j=0;i<=len1;i++){
while(j>0&&haystack[i]!=needle[j+1]){
j=ne[j];
}
if(haystack[i]==needle[j+1])j++;
if(j==len2){
return i-len2;
}
}
return -1;
}
};
边栏推荐
- Microsoft silently donated $10000 to curl, which was not notified until half a year later
- SAP UI5 FileUploader 的隐藏 iframe 设计明细
- 记一次 .NET 某智慧物流 WCS系统 CPU 爆高分析
- Measured: the performance of cloud RDS MySQL is 1.6 times that of self built
- 小于n的最大数
- Pointer elementary of C language
- Getting started with unity
- Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
- 移动端页面布局
- Xcode releases test package testflight
猜你喜欢

How to extract tables from PDF through C /vb.net

JSP El expression, JSTL tag

Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds

SAP UI5 FileUploader 的隐藏 iframe 设计明细

Day07 operation

Kubernetes Chapter 8: deploy NFS system with kubernetes to complete database persistence (kubernetes work practice class)

Global string object (function type) +math object

第7天总结&作业

Microsoft silently donated $10000 to curl, which was not notified until half a year later

App Crash收集和分析
随机推荐
Global string object (function type) +math object
Unity 入门
Shell编程规范与变量
Start from scratch blazor server (1) -- project construction
JSP El expression, JSTL tag
Passive income: return to the original and safe two ways to earn
Can deep learning overturn video codec? The first prize winner of the National Technological Invention Award nags you in the little red book
合工大苍穹战队视觉组培训Day7——视觉,jetson naon与D435i
通过 FileUploader 的初始化,了解 SAP UI5 应用的 StaticArea 初始化逻辑
Mobile page layout
Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
How does vs2019 C language run multiple projects at the same time, how to add multiple source files containing main functions in a project and debug and run them respectively
信通院陈屹力:降本增效是云原生应用最大价值
Node package depends on download management
移动端基础
Built in object (bottom)
Kubernetes Chapter 8: deploy NFS system with kubernetes to complete database persistence (kubernetes work practice class)
Complete steps of JDBC program implementation
基于STM32的智能鱼缸设计