String pattern matching Sunday algorithm

Source: Internet
Author: User

Text part turn from: http://www.cnblogs.com/mr-ghostaqi/p/4285868.html

I wrote the code myself.

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, the pattern string to the right to move the length of the pattern string +1 positions.

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: Ababcdababa

Pattern string: Ababa

That is, move to a character after D.

2. If the character appears in the pattern string, move to the right of the character's rightmost occurrence in the pattern string to the length of +1 at the end of the string.

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

1#include <iostream>2#include <stdio.h>3#include <string>4#include <string.h>5#include <algorithm>6#include <vector>7#include <map>8#include <stack>9#include <queue>Ten#include <math.h> One #defineMAXN 100100 A #defineN 22000 - using namespacestd; - intdis[ -]; the Char*STRSTR (Char*haystack,Char*needle) - { -        //Haystack indicates that the parent string -        //needle represents a substring +        intSlen =strlen (haystack); -        intPlen =strlen (needle); +  A        intdis[ -];//represents the distance skipped when a mismatch occurs at  -         for(inti =0; I < -; i++) -        { -Dis[i] = plen+1;//Initialize to substring length +1 -  -        } in  -         for(inti =0; I < plen;i++) to        { +Dis[needle[i]-'a'] = Plen-i; -        } the  *  $        ints =0;Panax Notoginseng        inti =s; -        intj =0; the  +  A         while(I < Slen&&j <Plen) the        { +            if(Haystack[i] = =Needle[j]) -            { $i++; $J + +; -            } -            Else the            { -                if(S + Plen < Slen)//to determine if an element of S + Plen existsWuyi                { the                    Charc = haystack[s+Plen]; -s = s + dis[c-'a']; Wui =s; -j =0; About                } $                Else -                { -                    returnNULL; -                } A            } +        } the  -        if(j = = Plen)returnhaystack+s; $        Else returnNULL; the  the  the } the intMain () - { in     Char* str ="a"; the     Char* p ="a"; the     Char* Q =NULL; AboutQ =strStr (str,p); the  the     if(q = NULL) puts ("NO"); the     if(q!=null) printf ("%s\n", q); +  -}
1#include <iostream>2#include <stdio.h>3#include <string>4#include <string.h>5#include <algorithm>6#include <vector>7#include <map>8#include <stack>9#include <queue>Ten#include <math.h> One #defineMAXN 100100 A #defineN 22000 - using namespacestd; - intdis[ -]; the //returns the first position that appears, otherwise returns null - Char*sunday (Char*haystack,Char*needle) - { -        //Haystack indicates that the parent string +        //needle represents a substring -        intSlen =strlen (haystack); +        intPlen =strlen (needle); A  at        intdis[ -];//represents the distance skipped when a mismatch occurs -  -         for(inti =0; I < -; i++) -        { -Dis[i] = plen+1;//Initialize to substring length +1 -  in        } to         for(inti =0; I < plen;i++) +        { -Dis[needle[i]-'a'] = Plen-i; the        } *  Panax Notoginseng        ints =0; -        inti =s; the        intj =0; the         while(I < Slen&&j <Plen) +        { -            if(Haystack[i] = =Needle[j]) $            { $i++; -J + +; -            } the            Else -            {Wuyi                if(S + Plen < Slen)//to determine if an element of S + Plen exists the                { -                    Charc = haystack[s+Plen]; Wus = s + dis[c-'a']; -i =s; Aboutj =0; $                } -                Else -                { -                    returnNULL; A                } +            } the        } -  $        if(j = = Plen)returnhaystack+s; the        Else returnNULL; the } - intMain () in { the     Char* str ="Ababcdababa"; the     Char* p ="Ababa"; About     Char* Q =NULL; theQ =strStr (str,p); the  the     if(q = NULL) puts ("NO"); +     if(q!=null) printf ("%s\n", q); -  the}

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.