当前位置:网站首页>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 .
边栏推荐
- 移动端——uniapp开发记录(公共请求request封装)
- Crazy scientist
- Bugku CTF daily question baby_ flag. txt
- FISCO bcos zero knowledge proof Fiat Shamir instance source code
- Jincang KFS data bidirectional synchronization scenario deployment
- Leetcode simple question: check whether the string is an array prefix
- Priv-app permission异常
- Hj35 serpentine matrix
- 文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
- Leetcode simple problem delete an element to strictly increment the array
猜你喜欢
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
Leetcode simple question: check whether two string arrays are equal
vulnhub HA: Natraj
The simple problem of leetcode: dismantling bombs
Asp access teaching management system design finished product
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
Joint search set: the number of points in connected blocks (the number of points in a set)
Truncated sentences of leetcode simple questions
会员积分商城系统的功能介绍
Network security textual research recommendation
随机推荐
Joint set search: merge intervals and ask whether two numbers are in the same set
Learning practice: comprehensive application of cycle and branch structure (I)
Kubernetes source code analysis (I)
Leetcode simple problem delete an element to strictly increment the array
[SQL injection] joint query (the simplest injection method)
Php+mysql registration landing page development complete code
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
金仓数据库KingbaseES 插件kdb_database_link
SSM based campus part-time platform for College Students
Some information about the developer environment in Chengdu
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
Dive into deep learning - 2.1 data operation & Exercise
MC Layer Target
并发操作-内存交互操作
stm32逆向入门
[dynamic programming] subsequence problem
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
Factor stock selection scoring model
Games101 Lesson 9 shading 3 Notes