Boyer-Moore algorithm for Pattern Matching

Source: Internet
Author: User

BMAlgorithmIs a better pattern matching algorithm. Generally, if the length of the pattern string is not considered, an algorithm with time complexity O (n) should be optimal, but this is not the case. The BM algorithm can achieve more efficient pattern matching. The analysis and experiment show that the BM matching algorithm is the most efficient for those character sets with fewer characters in the pattern string. Furthermore, considering the optimization of the KMP matching method, KMP matching and BM matching can be combined to further improve the efficiency.

Similar to KMP, the key of the algorithm is to construct an auxiliary array. However, unlike KMP, the size of the auxiliary array of the BM algorithm is only related to the size of the character set of the matching string (generally, the ASCII character set, 256 characters). Its content is related to the mode string, the content of the secondary array is the index of the mode string: Position [Patten [I] = I; is also a simple secondary array structure.

A simple example:

# Include <string. h>
# Include <stdio. h>
# Include <stdlib. h>

/* Secondary array, depending on the character set and. The default ASCII character set is used, with 256 elements */
# Deprecision Len 256
Int bmmatcher (char * s, char * P, int index, int position [])
/*
Parameter description:
Char * s: matching string
Char * P: mode string
Int index: the starting position of the pattern string matching. It is the index of the matching string.
Int position [] secondary array,
*/
{
Int Len = strlen (s );
Int I, j, nextindex;

I = strlen (P)-1;
J = index + strlen (P)-1;
For (; I> = 0; I --, j --)
{
If (s [J]! = P [I]) break;
}
If (I <0) return 0; /* Matched */
Else if (position [s [J]> 0) nextindex = index + I-position [s [J];
Else nextindex = index + 1;

If (nextindex> len-strlen (p) Return-1; /* The matching fails and the next match cannot be performed */
Else return nextindex; /* The match fails. The next match is required */
}

/* Test. lowercase characters are used for matching strings and mode strings */
Int main ()
{
Int position [Len] = {0 }; /* Auxiliary array */
Char * src = "it is just a test, what wocould you do? "; /* Match string */
Char * Patten = "What wocould ";/* Mode string */
Int I, nextindex, Index =-2, Pos = 0;

For (I = 0; I <strlen (Patten); I ++) /* Construct the secondary array, a key step, but it is very simple */
Position [Patten [I] = I;
Index = bmmatcher (SRC, Patten, 0, position );
While (! (Index =-1 | Index = 0 )) /* Loop matching until the matching is successful or the matching fails */
{
Nextindex = index;
Index = bmmatcher (SRC, Patten, nextindex, position );
}

If (Index =-1)
Printf ("can not find it \ n ");
If (Index = 0)
Printf ("find it, the index is: % d. \ n", nextindex );
System ("pause ");
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.