当前位置:网站首页>String matching: find a substring in a string
String matching: find a substring in a string
2022-07-03 04:39:00 【Domineering ocean】
string matching : Find a substring in the string
demand
Our usual software development , Especially embedded development , String matching is a very important algorithm . At present, there are many commonly used string matching algorithms , Here are some .
Concrete algorithm
Conventional methods
The string is stored in the fixed length sequential storage structure of the character array , You can use the count pointer to indicate the character position currently being compared between the main string and the mode string . The basic idea of the algorithm is : From the... Of the main string i The first character of the pattern string is compared with the first character of the pattern string . If equal , Then continue to compare the following characters ; Otherwise, the next character of the main string will be compared with the first character of the mode string . Know that the pattern string is compared , Represents that there is a pattern string in the main string .
Program
int index(string S,stringT,int pos)
{
int i,j;
i=pos;
j=1;
while(i<=S[0]&&j<=T[0])
{
if(S[i]==T[j])
{
++i;
j++;
}
else
{
i=i-j+2;
j=1;
}
}
if(j>T[0])
return i-T[0];
else return 0;
}
KMP Algorithm
KMP The algorithm is also called Knut — Maurice — Pratt operation , Is a very efficient string matching algorithm .KMP Algorithm is an improved string matching algorithm , The key is to use the information after matching failure , Reduce the matching times between pattern string and main string as much as possible to achieve the purpose of fast matching . This algorithm can be used in O(n+m) Complete the string pattern matching operation on the order of time . The idea of the algorithm is : Whenever there are different characters in the matching process , No backtracking pointer is required , It's about taking advantage of what you've got “ Partial matching ” The result is to turn the pattern to the right “ rolling ” After a distance as far as possible , Continue to compare .
First of all, we need to define a concept , The longest string before - suffix .
give an example , character string abcdab
A collection of prefixes :{a,ab,abc,abcd,abcda}
A collection of suffixes :{b,ab,dab,cdab,bcdab}
So the longest time ago - The suffix is ab.
and KMP The algorithm will be the longest before - The suffix concept is used in next Array .
next The meaning of the array values : Represents the string before the current character , What is the length of the same Prefix suffix . For example, if next [j] = k, representative j The length of the string before is the largest k The same Prefix suffix of .
This means that when a character mismatch , This character corresponds to next The value will tell you that the next matching step is , Where should the pattern string jump to ( Jump to the next [j] The location of ). If next [j] be equal to 0 or -1, Jump to the beginning of the pattern string , if next [j] = k And k > 0, Represents that the next match jumps to j A character before , Instead of jumping to the beginning , And specifically skip k Characters .
Program
First we need to ask next Array
typedef struct
{
char data[MaxSize];
int length; // String length
} SqString;
void GetNext(SqString t,int next[]) // By mode string t Find out next value
{
int i,k;
i=0;k=-1;
next[0]=-1;// There is no string before the first character , Given value -1
while (i<t.length-1)
{
if (k==-1 || t.data[i]==t.data[k]) //k by -1 When the characters are equal
{
i++;k++;
next[i]=k;
}
else
{
k=next[k];
}
}
}
KPM Algorithm
int KMPIndex(SqString s,SqString t)
{
int next[MaxSize],i=0,j=0;
GetNext(t,next);
while (i<s.length && j<t.length)
{
if (j==-1 || s.data[i]==t.data[j])
{
i++;j++; //i,j Each increase 1
}
else j=next[j];
}
if (j>=t.length)
return(i-t.length); // Returns the first character subscript of the matching pattern string
else
return(-1); // Returns the mismatch flag
}
follow-up
If you want to learn more about the Internet of things 、 Smart home project knowledge , Can pay attention to my Program design column .
After subscribing to the column , I can talk to you on WeChat official account .

It's not easy to write , Thank you for your support .
边栏推荐
- Dive into deep learning - 2.1 data operation & Exercise
- Priv app permission exception
- 一名外包仔的2022年中总结
- 2022-02-13 (347. Top k high frequency elements)
- [Thesis Writing] how to write the overall design of JSP tourism network
- 【工具跑SQL盲注】
- Arthas watch grabs a field / attribute of the input parameter
- 2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
- Web - Information Collection
- 2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
猜你喜欢

SSM based campus part-time platform for College Students

Two drawing interfaces - 1 Matlab style interface

2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units

Leetcode simple question: check whether the string is an array prefix

Youdao cloud notes

有道云笔记

Leetcode simple question: check whether the array is sorted and rotated

X-ray normal based contour rendering

MC Layer Target

JVM原理简介
随机推荐
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units
有道云笔记
Kubernetes源码分析(一)
Leetcode simple problem delete an element to strictly increment the array
Smart contract security audit company selection analysis and audit report resources download - domestic article
Design and implementation of JSP logistics center storage information management system
一名外包仔的2022年中总结
Reptile exercise 03
MC Layer Target
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
Introduction of pointer variables in function parameters
2.14 summary
Introduction to message queuing (MQ)
【SQL注入点】注入点出现位置、判断
Small program animation realizes the running lantern and animation object
JS multidimensional array to one-dimensional array
普通本科大学生活避坑指南
arthas watch 抓取入参的某个字段/属性