KMP algorithm, which is the simplest and best understanding of the KMP algorithm I see

Source: Internet
Author: User

Read the article from

Http://www.cnblogs.com/c-cloud/p/3224788.html

Good understanding of the method of seeking next (recommended to see the original)

Next implementation

1 voidMakenext (Const CharP[],intnext[])2 {3     intQ,k;//Q: Template string subscript; k: Maximum prefix length4     intm = strlen (P);//Template String Length5next[0] =0;//The maximum prefix length for the first character of a template string is 06      for(q =1, k =0; Q < m; ++Q)//For Loop, starting with the second character, and sequentially calculating the next value for each character7     {8          while(k >0&& p[q]! = P[k])//recursion to find out p[0] P[Q] The largest and the same prefix length k9K = 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's really not good to understandTen         if(P[q] = = P[k])//if equal, then the maximum same prefix length plus 1 One         { Ak++; -         } -NEXT[Q] =K; the     } -}

KMP implementation

1#include <stdio.h>2#include <string.h>3 voidMakenext (Const CharP[],intnext[])4 {5     intq,k;6     intm =strlen (P);7next[0] =0;8      for(q =1, k =0; Q < m; ++q)9     {Ten          while(k >0&& P[q]! =P[k]) OneK = next[k-1]; A         if(P[q] = =P[k]) -         { -k++; the         } -NEXT[Q] =K; -     } - } +  - intKmpConst CharT[],Const CharP[],intnext[]) + { A     intn,m; at     inti,q; -n =strlen (T); -m =strlen (P); - Makenext (p,next); -      for(i =0, q =0; I < n; ++i) -     { in          while(Q >0&& P[q]! =T[i]) -Q = next[q-1]; to         if(P[q] = =T[i]) +         { -q++; the         } *         if(q = =m) $         {Panax Notoginsengprintf"Pattern occurs with shift:%d\n", (i-m+1)); -         } the     }     + } A  the intMain () + { -     inti; $     intnext[ -]={0}; $     Chart[ -], p[ -]; -scanf"%s%s", t,p); -      theprintf"%s\n", T); -printf"%s\n", P);Wuyi     //Makenext (p,next); the KMP (t,p,next); -      for(i =0; I < strlen (P); ++i) Wu     { -printf"%d", Next[i]); About     } $printf"\ n"); -  -     return 0; -}

KMP algorithm, which is the simplest and best understanding of the KMP algorithm I see

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.