[Algorithm explain] KMP & EXKMP

KMP

KMP string matching algorithm as a well-known - and the first half of this article.

Seeking to explain the aims of the next array, and the reader.

 

First throw the code luoguP3375 KMP string matching [template]

#include<iostream>
#include<cstring>
using namespace std;
const int N=5000002;
int next[N];
string s1,s2;
void init(string s){
  for(int i=0,j=next[0]=-1,len=s.size();i<len;){
    if(j==-1||s[i]==s[j])
        next[++i]=++j;
    else j=next[j];
  }
}
int main(){
    cin >> s1 >> s2;
    init(s2);
    for(int i=0,j=0,len=s1.size();i<len;){
        if(j==-1||s1[i]==s2[j])
            ++i,++j;
        else j=next[j];
        if(j==s2.size())cout << i-s2.size()+1 << endl;
    }for(int i=1,len=s2.size();i<=len;++i)
        cout << next[i] << " ";
    return 0;
}

 

 

Let's see init initialization function.

void init(string s){
  for(int i=0,j=next[0]=-1,len=s.size();i<len;){
    if(j==-1||s[i]==s[j])
        next[++i]=++j;
    else j=next[j];
  }
}

Of course, written while also OK

 

First, next [i] array refers to a string of 0 ~ i s longest prefix real portion is equal to the true length of the suffix.

Around a bit.

True front / Suffix: i.e., the original string is not included in the front / suffix.

 

Such true prefix ABCAB are A, AB, ABC, ABCA

True suffixed with B, AB, CAB, BCAB

 

Because in the matching process if there are

 

i, j represents the current process interval is 0 ~ i, determining the length of the prefix and suffix j, i.e., the prefix 0 ~ j-1, the suffix i-j + 1 ~ i

In fact, the process is their own match.

 

Guess you like

Origin www.cnblogs.com/lsy263/p/11516623.html