BM algorithm (better String Matching Algorithm)

Source: Internet
Author: User

BM Algorithm Like the KMP algorithm, it also constructs an auxiliary pattern function to accelerate the matching speed. Compared with the KMP mode function, the BM mode function is simpler:

Void make_next (const char P [], int next [])
{
For (INT I = 0; I <strlen (p); I ++)
Next [p [I] = I;
}

Next [] is an array of 256 data records, which is the same as the ASCII number. Of course, if a duplicate character appears, it is recorded as the final position of the character. In the preceding mode function, next [] is set to the ASCII position corresponding to the characters displayed in the security picture. # 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; // minus 1 because you need to remove the last '/0'
J = index + strlen (P)-1; // Index = 0 when bmmatcher is called for the first time, because the following for loop is compared from the end of the pattern string, therefore, the initial comparison position of the matching string should start with the length position of the number mode string.
For (; I> = 0; I --, j --)
{
If (s [J]! = P [I]) break;
} If (I <0) // I <0 indicates that the pattern string has been traversed. Return 0;/* matching successful */
Else if (position [s [J]> 0) // when a mismatch occurs, check whether the character at the current position of the matched string appears in the pattern string. Nextindex = index + I-position [s [J]; // Index indicates the starting offset of the current matching string, And I indicates the number of remaining comparison strings of the pattern string, position [s [J] is the position of the first unmatched character in the matching string. In this case, else nextindex = index + 1 is compared starting from the position where s [J] appears in the matching string. 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};/* Secondary array */
Char * src = "it is just a test, what wocould you do? ";/* Matching string */
Char * Patten = "What wocould";/* mode string */
Int I, nextindex, Index =-2, Pos = 0; for (I = 0; I <strlen (Patten); I ++)/* to construct a secondary array, the key step is 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.