当前位置:网站首页>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 .
边栏推荐
- The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
- Why does I start with =1? How does this code work?
- When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
- [set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
- [luatos sensor] 1 light sensing bh1750
- Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
- Function introduction of member points mall system
- 普通本科大学生活避坑指南
- 逆袭大学生的职业规划
- 关于开学的准备与专业认知
猜你喜欢
![[luatos sensor] 1 light sensing bh1750](/img/70/07f29e072c0b8630f92ec837fc12d5.jpg)
[luatos sensor] 1 light sensing bh1750
![[USACO 2009 Dec S]Music Notes](/img/e6/282a8820becdd24d63dcff1b81fcaf.jpg)
[USACO 2009 Dec S]Music Notes

Basic use of continuous integration server Jenkins

Joint set search: merge intervals and ask whether two numbers are in the same set

2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical

Leetcode simple problem delete an element to strictly increment the array

会员积分商城系统的功能介绍

P35-P41 fourth_ context

联发科技2023届提前批IC笔试(题目)

2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
随机推荐
Number of 1 in binary (simple difficulty)
Small program animation realizes the running lantern and animation object
2.14 summary
[set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)
Leetcode simple question: check whether the string is an array prefix
vulnhub HA: Natraj
Contents of welder (primary) examination and welder (primary) examination in 2022
Pyqt control part (II)
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
金仓数据库KingbaseES 插件kdb_date_function
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
[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)
STM32 reverse entry
Truncated sentences of leetcode simple questions
Asp access teaching management system design finished product
【XSS绕过-防护策略】理解防护策略,更好的绕过
JS multidimensional array to one-dimensional array
Triangular rasterization
How to choose cross-border e-commerce multi merchant system