String Lookup KMP algorithm

Source: Internet
Author: User

If you've ever used ctrl+f, you have a great chance of using this algorithm, which is to find the pattern string (smaller, possibly a few characters) in the string to be found (which may have thousands of characters) and possibly find a position greater than or equal to 1 times. For example, find ABC in ABABCD. The idea of the algorithm is introduced here, giving only the first occurrence of the position.

I. Algorithmic thinking

The traditional algorithm starts with the first character of the matching string and compares the pattern string until it encounters a non-conforming character, and then begins with the next character of the matching string, repeating the above procedure. The code is as follows:

voidFindCharT[],Charp[]) {    intm =strlen (t); intn =strlen (P); intI,j,k;//k: Matching string subscript, J: pattern string subscript     for(k=0; k<m;k++) {J=0; I=K;  while(j<N) {            if(p[j]==T[i]) {i++; J++; }Else{                 Break; }        }        if(j==N) {printf ("match at%d \ n", K);  Break; }    }}

The KMP algorithm is an improvement in this algorithm, which is not that I do not move one position at a time, but rather move backwards as much as possible to improve the matching efficiency. How many locations to move, this is the key to the KMP algorithm. The KMP algorithm maintains an array of the same length as the pattern string, which represents the maximum prefix length for the current match. For example, the maximum prefix length for Abacab is 2, prefix ab, suffix AB, respectively. And the array next is [0,0,1,0,1,2], you can use this information to directly skip the prefix that has been matched.

Two. Algorithm implementation

voidMakenext (CharP[],intnext[]) {    intQ,k;//k is the maximum prefix length, q is the matching string subscriptnext[0] =0;  for(q=1, k=0; Q<strlen (p); q++){        //If they are not equal, look at the longest prefix of the last string, and so on         while(k>0&&p[q]!=P[k]) {k= next[k-1]; }        if(p[q]==P[k]) {k++; } Next[q]=K; }}voidKmpCharT[],Charp[]) {    intnext[3] = {0};    Makenext (P,next); intI=0, j=0;//I is the subscript of the matched string, and J is the subscript of the pattern string.     while(I<strlen (t) &&j<strlen (P)) {        //if equal, continue to compare        if(j==0|| p[j]==T[i]) {i++; J++; }Else{//not equal will jumpj = next[j-1]; }    }    if(j>=strlen (P)) {printf ("pattern string match at%d \ n", I-j); }Else{printf ("the match failed."); }}

String Lookup KMP algorithm

Related Article

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.