String pattern matching Sunday algorithm

Source: Internet
Author: User

Today, when I was doing leetcode, I ran into a string-matching topic:

https://oj.leetcode.com/problems/implement-strstr/

I was a little confused, string pattern matching I remember when in the data structure, the book only wrote the BF and KMP algorithm, the teacher said the exam "only may be the BF", KMP do not require mastery.

Then out of a search for the heart, I still looked at the KMP, the algorithm is very difficult to understand, so it was not written down.

A look at the problem on his knees.

On the internet to check some algorithms, as if all of the Sunday algorithm is very respected look, so looked for a few look, algorithm thinking is very simple, mathematical proof I also lazy to understand, after all, I am not learning math material.

The basic idea of the algorithm is that the pattern string and the main string are compared from backward to forward, when encountering the characters that cannot be matched, see the main string to match the last character of the next character , and then in two cases:

1. If the character does not appear in the pattern string, move the pattern string to the right and the length of the pattern string + one position .

For example: main string: AbabcDAbaba

Pattern string: Ababa

The position of C does not match, see D after C does not appear in the pattern string, then move to the right 5+1 position, the result is:

main string: Ababcd Ababa

Pattern string: Ababa

That is, move to a character after D.

2. If the character appears in the pattern string, move to the right " The right-most time the character appears in the pattern string "to the character The length of the end of the string is +1.

For example: main string: AbabCAbaba

Pattern string: Ababa

To the position of C can not match, see C after a appears in the pattern string, and the pattern string has 3 A, we see the rightmost a, the right to move 0+1 position, the result is:

Main string: AbabCAbaba

Pattern string: Ababa

The specific implementation of C + + code is as follows, the match successfully returns the subscript of the first character successfully, and returns 1 if unsuccessful;

1 intSTRSTR (Char*haystack,Char*needle)2 {3     intLen_h = strlen (haystack);//the length of the main string4     intLen_n = strlen (needle);//the length of the pattern string5     intnext[ -] = {0};//The distance the pattern string moves to the right when the match is unsuccessful, and each letter corresponds to a distance6      for(inti =0; I < -; i++)7     {8Next[i] = Len_n +1;//The default is the length of the pattern string +19     }Ten      for(inti =0; i < Len_n; i++) One     { ANext[needle[i]-'a'] = Len_n-i;//Modify the moving distance based on the position of each character in the pattern string -     } -     intindex =0; the      while(Index <= (len_h-len_n))//Start Matching -     { -         inti =index; -         intJ; +          for(j =0; J < Len_n; i++, J + +) -         { +             if(Haystack[i]! = Needle[j])//if the match is unsuccessful A             { at                 if(Index + len_n) >= Len_h)//the discriminant here is to prevent excessive moving distances, causing the array to go out of bounds -                     return-1; -Index + = Next[haystack[index + Len_n]-'a'];//The pattern string moves to the right according to the position of the right character corresponding to the main string match in the next array.  -                  Break; -             } -         } in         if(J = =len_n) -         { to             returnindex; +         } -     } the     return-1; *}

If the code has any questions and suggestions welcome to the point.

String pattern matching Sunday 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.