KMP Algorithm Learning Notes

Source: Internet
Author: User

http://www.matrix67.com/blog/archives/115

Orz maxtix67

has been dragged to meet the KMP algorithm of the problem of the intellect.

With string a:abefgababef

Pattern String B:ABEFGABEF

When A and B match, set I is a pointer on a, j is a pointer on B, then A and B match to the 8th bit will be mismatch. Following the usual method, we will jump back to 2,j and jump back to 1 to match. After observation we will find that this is superfluous, because the middle of the BEFG is not possible, we should jump directly to the 6th bit of a to match.

The KMP algorithm accelerates the matching speed by filtering out such useless characters.

Take the above B for example, when the abefgab has been matched and the next mismatch, in fact, the following AB can be directly used as the beginning of a new match, because AB is the abefgab prefix is the suffix. This gives us the idea that if we can find the largest substring of the matched string, which is the suffix (called the prefix), we can directly put him in the next match, which reduces the complexity of the time. The question now is how to find the maximum prefix for each of the pattern strings.

S[i] is the first prefix of the pattern string, that is, the string consisting of the first I bit, p[i] is the maximum prefix length of s[i], assuming we know p[i], requires p[i+1], in fact, this is a pattern string itself matching process. If S[I+1]==S[P[I]+1] is the largest prefix and suffix as the next match on the p[i+1]=p[i]+1, obviously, if not equal, the second to find P[i] The largest prefix, so that has been retired, there will always be a match on, otherwise p[i+1 ] is 0. So we get an array of p, which can be modeled after the process of P.

Get the P array:

void Getp () {j=0;p[0]=0;for (i=1;i<m;i++) {while (J&&b[j]!=b[i]) j=p[j-1];if (B[j]==b[i]) j + +;p [I]=j;}}
codevs1404:

#include <iostream> #include <cstdio> #include <memory.h> #define MAXN 200005char A[maxn],b[maxn];int I,j,n,m,k,x,p[maxn],len[maxn],cnt[maxn];void Getp () {j=0;p[0]=0;for (i=1;i<m;i++) {while (J&&b[j]!=b[i]) J=p[j-1];if (B[j]==b[i]) j + +;p [I]=j;}} void Match (char *a,char *b) {int i,j=0;for (i=0;i<n;i++) {while (J&&b[j]!=a[i]) j=p[j-1];if (B[j]==a[i]) j + +; Cnt[j]++;if (j==m) j=p[j-1];} int main () {scanf ("%d%d%d\n%s\n%s", &n,&m,&k,&a,&b); Getp (); memset (CNT); match (A, b); for (i=m;i>=1;i--) cnt[p[i-1]]+=cnt[i];for (i=1;i<=k;i++) {scanf ("%d", &x);p rintf ("%d\n", cnt[x]-cnt[x+ 1]);}}



KMP Algorithm Learning Notes

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.