Pits [Main Line Task] MP&KMP algorithm

Source: Internet
Author: User

At that time have not understood Qaq, think will hash is enough, but think of the fail array has a lot of magical magic, so to fill this hole

First, the MP algorithm:

I'll just say the meaning of the fail array (starting with the 0 string subscript):

Fail[i] Represents the length of the longest border of the 0~i-1 string, and it is also the position subscript that will match the beginning of the match.

Give me a little chestnut: for string abcdabd

Fail[6]=2, that is, the longest border length of the Abcdab is 2 (AB), when the match fails, the subscript jumps to Fail[6] (2) to match, that is, the ' C ' letter begins to match (because the front ' AB ' has been matched successfully)

For example, this match:

When the match ' d ' with the space fails, fail[6]=2, jump to 2 again to take ' C ' to the space match, and ' C ' front of ' AB ' has been matched successfully

So our matching code is this:

void work () {    int j=0;    POS (i,0,len2-1) {while        (J&&p[j]!=q[i]) j=fail[j];//always matches without jumping forward until the beginning or the match succeeds         if (P[j]==q[i]) j++;/ /Match succeeded to continue backwards matching         if (j==len) {            ans++;            j=fail[j];//if the full string match succeeds, jump directly to the full string of fail and start the match again}}    

So how do we find the fail array? It's just that you match yourself.

We already know fail[i-1], that we know 0~i-2 the longest border, then we beg Fail[i], will take p[i-1] and fail[i-1] to compare, if equality is fail[i]=fail[i-1]+1, if not equal is the equivalent of mismatch, Go forward and fail until you jump to the beginning or find a place to match.

Code implementation:

void Getfail () {Len=strlen (P); Fail[0]=fail[1]=0;int K=0;pos (I,2,len) {while (k&&p[k]!=p[i-1]) k=fail[k];if (p [k]==p[i-1]) k++;fail[i]=k;}}

KMP algorithm:

In the MP algorithm, the fail is the longest BORDER,KMP algorithm has a trace of the difference, that is, the fail array is the best border

The code implementation adds 1 lines:

void Getfail () {    fail[0]=fail[1]=0;    int k=0;    POS (I,2,len) {        while (k&&p[k]!=p[i-1]) k=fail[k];        if (p[k]==p[i-1]) k++;        if (P[k]!=p[i]) fail[i]=k;        else fail[i]=fail[k];}    }

That is to say we fail to match, we need to compare the fail and the current position, the equivalent of reference to the previous experience

This means that if we have matched the next match, it is obviously doomed to fail, so our fail jumps forward one to achieve the optimal purpose QVQ

Next again is the happy brush question:

Pits [Main Line Task] MP&KMP 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.