The two most famous character string search algorithms are KMP algorithm (knuth-Morris-Pratt) and BM algorithm (Boyer-Moore). In the worst case, both algorithms have linear search time. However, in practice, the KMP algorithm is not much faster than the simplest C-database function strstr (), while the BM algorithm is usually 3-5 times faster than the KMP algorithm.
However, in the worst case, the time complexity of BM seems to be n × n.
Not to mention the specifics. The BM algorithm uses the main text string to quickly perform non-backtracking search. The beating algorithm is implemented using this sentence in the program. below:
- I = I + M-min (J, 1 + last (P, T [I]);
Last is the final position of the character in the search string.
This algorithm is very troublesome. If you can, Baidu.
The Code is as follows:
- # Include <string. h>
- Int last (char * P, char c) {// locate the position where c finally matches in P. If no position is found,-1 is returned.
- Int length = strlen (P), Count = 0;
- Char * PP = P + Length-1;
- While (PP> = P)
- {
- If (* PP = C)
- {
- Return length-count-1;
- }
- PP --;
- Count ++;
- }
- Return-1;
- }
- Int min (int A, int B ){
- Return (A <= B )? A: B;
- }
- Int bm_index (char * t, char * P ){
- Int n = strlen (t );
- Int M = strlen (P );
- Int I = s-1, j = m-1;
- While (I <= N-1)
- {
- If (T [I] = P [J])
- {
- If (j = 0)
- {
- Return I;
- }
- Else
- I --, j --;
- }
- Else {
- I = I + M-min (J, 1 + last (P, T [I]); // jump back, depending on the position of the last matched character
- J = m-1;
- }
- }
- Return-1;
- }
- Int _ tmain (INT argc, _ tchar * argv [])
- {
- Char * P = "woainizz! Izzzzzz -- zzzzzzut ";
- Int A = bm_index (P, "zzzzut"); // result 18, no problem
- Return 0;
- }