The solution idea of next array of KMP algorithm

Source: Internet
Author: User

Solving ideas of 2.next arrays

This section of the content is transferred from: http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html

Through the above can completely understand the principle of the KMP algorithm, then the next step is the implementation of the program, the most important thing is how to match the template string to find the corresponding maximum length of the same prefix for each bit. I will give my code first:

1void Makenext (ConstChar p[],IntNext[])2{3int q,k;//Q: Template string subscript; k: Maximum prefix length4int m = strlen (P);//Template string length5 next[0] =0;//The maximum prefix length for the first character of a template string is 06for (q =1,k =0; Q < m; ++Q)//For loop, starting with the second character, and sequentially calculating the next value for each character7{8while (K >0 && p[q]! = P[k])//Recursion to find out p[0] P[Q] The largest and the same prefix length k9 k = next[k-1]; // don't understand it's okay look at the following analysis, this while loop is the essence of the whole code, it is really not good understanding 10 if (p[q] = = P[k]) // If equal, the maximum same prefix length plus 111 12 k++;}14 Next[q] = K;15 }16}                 

Now I'll focus on the work done by the while loop:

    1. It is known that the maximum prefix length is K (k>0) when the previous step is calculated, i.e. p[0] P[K-1];
    2. At this time compare the K-item p[k] and p[q],1 as shown
    3. If p[k] equals p[q], then it is simple to jump out of the while loop;
    4.   The Key! The key is wood! What if the key is not equal??? then we should take advantage of the next[0] we've got. Next[k-1] to beg for p[0] P[K-1] This substring is the largest of the same prefix , there may be classmates to ask-why ask p[0] P[K-1] The largest and the same prefix??? Oh, yes! Why is it? The reason is that p[k] has been mismatch with p[q] and p[q-k] P[q-1] again with p[0] P[k-1] The same, it seems p[0] P[K-1] So long the substring is not used, then I want to find a same also p[0] beginning, p[k-1] end of the substring that is p[0] P[j-1] (J==next[k-1]) to see if its next p[j] matches p[q]. 2 is shown

Attached code:

1 #include <stdio.h>2 #include <String.h>3void Makenext (ConstChar p[],IntNext[])4{5IntQ,k;6int m =Strlen (P);7 next[0] =0;8for (q =1,k =0; Q < m; ++Q9{10while (K >0 && p[q]! =P[K])K = next[k-1];12if (p[q] = =P[K])13{k++;15}NEXT[Q] =K17}18}1920int KMP (ConstChar t[],ConstChar p[],IntNext[])21st{22IntN,m;23IntI,q;n =Strlen (T);m =Strlen (P);26Makenext (P,next);27for (i =0,q =0; I < n; ++I28{29while (Q >0 && p[q]! =T[i])Q = next[q-1];31if (p[q] = =T[i])32{q++;34}35if (q = =M36{Notoginseng printf ("Pattern occurs with shift:%d\n", (i-m+1));38}39}40}4142IntMain ()43{44IntI45int next[20]={0};46Char t[] ="Ababxbababcadfdsss";47Char p[] ="Abcdabd";printf ("%s\n", T);* printf ("%s\n", P);50//Makenext (P,next);51KMP (T,p,next);52 for (i = 0; I < Strlen (P); ++i)  {54 printf ( "%d " ,next[i]); 55 }56 printf ( "\n ) 57 58 return 0; 59}             

The solution idea of next array of 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.