KMP algorithm (detailed next[n of strings)

Source: Internet
Author: User

How to find the pattern value of a string Next[n]

(1)next[0]=-1 meaning: The pattern value of the first character of any string is set to -1.


(2)next[j]=-1 meaning: The character of the pattern string T labeled J , if with the first character

The 1-k characters in front of J and the beginning of the 1-k

characters (or equal but t[k]==t[j]) (1≤k<j).

such as:t= "Abcabcad" is next[6]=-1, because t[3]=t[6]


(3)next[j]=k meaning: The character of the pattern string T is labeled J , if the front K of J

The characters are equal to the first K characters, and t[j]! = T[k] (1≤k<j).

IE t[0]t[1]t[2]... t[k-1]== t[j-k]t[j-k+1]t[j-k+2] ... T[J-1]

and t[j]! = t[k]. (1≤k<j);


(4) next[j]=0 meaning: In addition to (1) (2) (3) other circumstances.


#include <iostream.h>//#include <string.h> void get_nextval (const char *t, int next[]) {//Ask the next function of the pattern string T       Value and deposit the array next.       Int j = 0, k =-1;       Next[0] =-1; while (t[j]! = ' + ') {if (k = =-1 | |                     T[J] = = T[k]) {++j; ++k;                     if (T[j]!=t[k]) next[j] = k;              else next[j] = next[k];       } else K = next[k];       }//Here is my Add display section for (int i=0;i<j;i++) {cout<<next[i]<<endl;   } Cout<<endl;} int KMP (const char *text,const char* Pattern) {if (! Text| |!  pattern| |  pattern[0]== ' | |       text[0]== ' + ') return-1;//null pointer or empty string, return-1.       int len=0;       const char * c=pattern;         while (*c++!= ') {++len; } int *neXt=new int[len+1];       Get_nextval (Pattern,next);//Find the next function value of Pattern int index=0,i=0,j=0;                     while (text[i]!= ' && pattern[j]!= ') {if (text[i]== Pattern[j]) {                      ++i;              ++j;                     } else {index + = J-next[j];                            if (next[j]!=-1) j=next[j];//mode string move right else {                            j=0;                     ++i;       }}} delete []next;      if (pattern[j]== ') return index;//match success Else return-1;       }int Main () {char* text= "babadcadcaaaaa";               Char*pattern= "Adcad";       COUT&LT;&LT;KMP (Text,pattern) <<endl; return 0;}


KMP algorithm (detailed next[n of strings)

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.