pattern matching (exemplary match): The positioning of substrings in the main string is called pattern matching or string matching. Pattern matching success refers to the ability to find the pattern string T in the main string s, otherwise, the called pattern string T does not exist in the main string s.
Two common pattern-matching algorithms are described below:
- brute-force pattern matching algorithm storm algorithm, also known as Brute force algorithm.
The core idea of the algorithm is as follows:
Set S as the target string, T is the pattern string, and may be set:
S= "S0s1s2...sn-1", t= "t0t1t2 ... tm-1"
A string match is actually a comparison of a valid position 0≦i≦n-m the substring of the target string s[i...i+m-1] and the pattern string t[0...m-1] in turn:
- If S[I...I+M-1]=T[0...M-1]: it is said that the match starting from position I is successful, also known as the pattern T in the target s appears;
- If S[I...I+M-1]≠T[0...M-1]: the match from I failed. Position I is called displacement, when s[i...i+m-1]=t[0...m-1], I is called effective displacement; when s[i...i+m-1]≠t[0...m-1], I is called invalid displacement.
The algorithm is implemented as follows:
(The author lazy, implemented in C #, in fact, C # String type has been encapsulated to implement the function)
1 Public StaticInt32 IndexOf (String parentstr, string childstr)2 {3Int32 result =-1;4 Try5 {6 if(Parentstr.length >1&& childstr.length >1)7 {8Int32 i =0;9Int32 j =0;Ten while(I < parentstr.length && J <childstr.length) One { A if(Parentstr[i] = =Childstr[j]) - { -i++; theJ + +; - } - Else - { +i = I-j +1; -j =0; + } A } at if(I <parentstr.length) - { -result = i-J; - } - } - } in Catch(Exception) - { toresult =-1; + } - returnresult; the}
The time complexity of the algorithm is O (n*m), where N and M are the lengths of the main string and the pattern string, respectively.
2. KMP algorithm , the algorithm is an improved algorithm for the above-mentioned storm algorithm, and its improvement lies in:
Each time a match occurs when the characters are not equal, the main string indicator does not backtrack, but instead uses the resulting "partial match" result to "slide" the indicator of the pattern string to the right to a distance as far as possible to continue the comparison. Core idea: "The use of already partially matched this effective information, keep i pointer does not backtrack, by modifying the J Pointer, let the pattern string as far as possible to move to a valid position ." "
Two pattern matching algorithms for strings