KMP algorithm I am watching July blog study, here just make a note, detailed content see July blog:http://blog.csdn.net/v_july_v/article/details/7041827

The purpose of the KMP algorithm: There is a text string s and a pattern string p, now to find the position of P in S. The brute force matching algorithm needs to backtrack the text string s, the KMP algorithm is to make the text string not fallback, only need to move the mode string J.

**KMP Algorithm general idea:** is when s[i]==p[j], we to i++,j++, if not equal j=next[j]. Here Next[j] is the same prefix suffix for the length of the string before the J character. Equivalent to moving the pattern string to the right j=next[j] bit. The next array is solved using the idea of iteration, need to divide p[j] is equal to p[k] two cases to obtain next[j+1].

**time Complexity:** The time complexity of the brute-force matching algorithm is O (n*m) (worst case). The time complexity of the KMP algorithm is O (n+m). Here N and M are the lengths of the text string and the string, respectively.

**optimize next array:**next array mainly for the case of the pattern string abab, such as in the text string Abacab, when the p[3] and S[3] match failed, then abab to the right to move two bits, at this time p[1] is still equal to B, matching must fail, The next array of optimizations mainly solves this problem. When P[3]=p[next[3]] [seasonal next[3]=next[next[3]]. Reading the code is a good idea.

**The above is a personal understanding of the notes, the following gives the specific code:**

1:KMP Algorithm Code

#include <iostream>using namespace std;//get next array void getNext (int *next, const char *p)//known next[j] request Next[j +1] Whether P[j] is equal to p[k] the thought of the iteration of both cases {next[0] = -1;int k = -1;int j = 0;while (J < strlen (p)-1) {if (k = =-1 | | p[j] = = P[k]) {k++;j+ +;NEXT[J] = k;} Else{k = Next[k]; Continue iteration}}}//get optimized next array void getnextopt (int *next, const char *p)//known next[j] next[j+1] Sub p[j] is equal to p[k] the thought of the iteration of the two cases { Next[0] = -1;int k = -1;int j = 0;while (J < strlen (p)-1) {if (k = =-1 | | p[j] = = P[k]) {k++;j++;if (p[j]! = P[k])//n Ext array is optimized for abab such as no need to repeat next[j] = k;else Next[j] = next[k];} Else{k = Next[k]; }}}//KMP Algorithm int kmpsearch (const char *p, const char*s) {int ptrlen = strlen (p); Gets the length of the pattern string and text string int strLen = StrLen (s); int *next = new Int[ptrlen]; Dynamic application Next Array GetNext (next, p); Get next array int i = 0, j = 0;while (I < StrLen && J < Ptrlen) {if (j = =-1 | | s[i] = = P[j]) {i++;j++;} else if (j = = 0)//so that the first character is not satisfied when the direct optimization i++;else{j = Next[j];}}delete[] Next; Release the array of dynamic requests if (j = = Ptrlen) return i-j;else return-1;} int main () {char *ptr = "ABCD"; char *str = "ABABABCD"; cout << kmpsearch (PTR, str) << Endl;return 0;}

2: Brute force matching algorithm

#include <iostream>using namespace Std;int violencesearch (const char *p, const char *s) {int ptrlen = strlen (p); gets the length of the pattern string and text string int strLen = StrLen (s); int i = 0, j = 0;while (I < StrLen && J < Ptrlen) { if (s[i] = = P [j]) {i++;j++;} Else{i = I-j+1;j = 0;}} if (j = = Ptrlen) return i-j;else return-1;} int main () {char Ptr[100];char str[100];cin >> ptr >> str;cout << violencesearch (PTR, str) << Endl; }

3: Calculating the maximum length table algorithm

gets the same prefix suffix int getmaxfix (const char *p, int plen) of the maximum length for a string {int k =plen/2;while (k > 0) {//bool flag = False;int I = 0, J = plen-k;while (I < k) {if (p[i] = = P[j]) {i++;j++;} Else{//flag = True;k--;break;}}} return k;} get the maximum length table void getmaxtable (const char *p, int *table) {for (int i = 0; i < strlen (p); i++) {Table[i] = Getmaxfix (p, i+ 1);}}

Reference: http://blog.csdn.net/v_july_v/article/details/7041827

Small village Source:http://blog.csdn.net/lu597203933 Welcome to reprint or share, but be sure to declare the source of the article. (Sina Weibo: Small mayor Zack, Welcome to Exchange!)

KMP Algorithm Notes