Sunday algorithm of string matching

Source: Internet
Author: User

Sunday algorithm is not as complex as KMP algorithm, but the efficiency is relatively high, on the KMP above, the following simple introduction Sunday algorithm and its implementation.

The Sunday algorithm, proposed by Daniel M.sunday in 1990, is similar to the BM algorithm:

Except that the Sunday algorithm matches the previous one, and when the match fails, it is the next character in the text string that participates in the matching last digit.


If the character does not appear in the pattern string, skip directly, that is, move the number of digits = Match string length + 1;
Otherwise, its number of moves = the distance +1 from the right end of the pattern string to the end of the character, so that the next character is aligned with its equal character in the pattern string.


Here is an example to illustrate the next Sunday algorithm. Suppose you now look for the pattern string "search" in the text string "substring searching algorithm".

1. At first, align the pattern string to the left of the text string:

2. The result found that a mismatch was found at the 2nd character, and the next bit of the last character in the text string that was participating in the match was not matched, that is, the bold character I, because I was not present in the pattern string search.
So the pattern string jumps directly over a large area, moving the number of digits to the right = matching string length + 1 = 6 + 1 = 7, starting with the character after I (that is, the character N) to start the next match, such as:

3. The result of the first character does not match, and then look at the text string to participate in the match of the last character of the next character, is ' R ', it appears in the pattern string in the bottom 3rd, so the pattern string to the right 3-bit (R to the end of the pattern string distance + 1 = 2 + 1 =3), the following:

4. Successful match.

The following is the JavaScript implementation of the Sunday algorithm

functionSunday (source, pattern) {varSlen =Source.length, Plen=pattern.length; varSindex = 0, Pindex= 0, loc= 0;  while(Sindex < Slen && Pindex <Plen) {        //The index is equal, and the comparison is continued backwards        if(Source[sindex] = =Pattern[pindex]) {Sindex++; Pindex++; }         Else {            //Not Equal,jump            varAimchar = Source[sindex +Plen], Paim= PLen-1; //The index of the index , equal to the value of the next bit of the string participating in the match, is indexed in the pattern string             while(Paim > 0) {                if(Aimchar = =Pattern[paim]) {                     Break; } Paim--; }            //Jump,plen-paim is the value sindex should advance, Sindex from 0 .Sindex + = Plen-Paim; //Record LocationLOC =Sindex; //Reset to zeroPindex = 0; }    }    if(Pindex <Plen) {        return-1; }    returnLoc;}

Because the Sunday algorithm moves a lot in each step, the efficiency is very high.

Reference: http://wiki.jikexueyuan.com/project/kmp-algorithm/sunday.html

http://blog.csdn.net/silver_sail/article/details/8137782

Sunday algorithm of string matching

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.