C + + Sundy algorithm (improved BM algorithm)

Source: Internet
Author: User

The most famous two of the string lookup algorithms are the KMP algorithm Knuth-morris-pratt) and BM Algorithm (Boyer-moore). Two algorithms in the worst situation

time to find a linear search condition. The BM algorithm is often 3-5 times faster than the KMP algorithm. But the BM algorithm is not the fastest algorithm, here is a faster than the BM algorithm search algorithm.

For example, we want to find "search" in "Substringsearchingalgorithm"

The first step is to align the string to the left of the text:

S u B s t r I N g S e a r c h i n g a l g o r i t h M

S e a R c h

The result finds a mismatch at the second character, so the string moves backwards.

But how much does it move?

The simplest approach is to move a character position;

KMP is to use the information that already matches the part to move;

The BM algorithm is done in reverse comparison, and according to the already matched section to determine the amount of movement.

The Sundy algorithm is to see the character immediately following the current substring (section a string of ' I ').


Obviously, no matter how much the move, this character is definitely going to take the next step in the comparison, i.e., The next match is made, and the character must be within the substring.

So, you can move the substring so that the substring in the string The rightmost character of this is aligned with it.

Now that there is no ' I ' in the substring ' search ', you can

To jump directly over a large area, starting with the character after ' I ', the next comparison is as follows:

s U B s t r I N g S e aR c h i n g a l g o r i t h M

s e a R c h

The result of the comparison, the first character does not match, then look at the substring behind the character, is ' R ',

It's in the sub The string appears in the third-to-last position, so the string is moved backwards three bits, so that two ' r ' alignment, as follows:

s U B s t r i n g s e a r c h i n g A l g o r i t h M

S e a r c h

This match was successful! Looking back at the whole process, we just moved two substrings to find the matching position,

It can be proved that with this algorithm, each step of the movement is larger than the BM algorithm , the to be sure is faster than the BM algorithm.


Here is the implementation code:

#include <iostream> #include <string>using namespace std;void SUNDAY (char *text, char *patt) {Register size_t temp[256];size_t *shift = temp;size_t I, patt_size = strlen (Patt), text_size = strlen (text); cout << "Size:" <&L T Patt_size << endl;for (i=0; i < i++) {* (shift+i) = patt_size+1;} for (i=0; i < patt_size; i++) {* (Shift + (unsigned char) (* (patt+i))) = Patt_size-i;} shift[' s ']=6 step, shitf[' e ']=5 and so on size_t limit = Text_size-patt_size+1;for (i=0; i < limit; i + = shift[text[i+patt_si Ze]] {if (text[i] = = *patt) {Char *match_text = text + i + 1;size_t match_size = 1;do{//outputs all matching locations if (match_size = = Patt_ Size) {cout << "the No. Is "<< i << Endl;}} while ((*match_text++) = = patt[match_size++]);}} cout << Endl;} int main (void) {char *text = new Char[100];text = "substring searching algorithm search"; char *patt = new CHAR[10];p att = " Search "; SUNDAY (text, Patt); return 0;}



C + + Sundy algorithm (improved BM algorithm)

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.