Source: Kmp.cpp
//KMP.cpp:Defines the entry point for the console application.//#include"stdafx.h"#include<IOSTREAM>using namespacestd;#defineBuf_size 100#defineBuf_size_2 200/************************************************************************//*KMP Algorithm*//************************************************************************///************************************//Method:compute Next Array//FullName: Calculate next array//Access:public//returns:void//Qualifier://Parameter:char t[] mode string//Parameter:int next[] Next array//parameter:int Length pattern string lengths//************************************voidCalu_next_array (CharT[],intNext[],intlength) { intj =0, k =-1; next[0] = -1; //Next[0] makes the pattern string T and the main string s each forward before further, 1 as a special flag//Next array size should be the pattern string T length//J Loop from 0 to Length-2, with n-1 secondary value plus next[0]=-1, total n-th value while(J < Length-1) { if(k = =-1|| T[J] = =T[k]) { //If k=-1, this is the first time, and only once will appear//when K=-1, there is k=0,j=1,next[1]=0, next[1]=0 is also constant//The recursive formula shows that the initial situation: Next[0]=-1,next[1]=0//******************* Recursive formula derivation *******************//if it is t[j] = = T[k], then there are://"Derivation: Set Next[0]...next[j] already find out, next[j+1]=?" "//Next[j] = k, you can launch t[0 ... k-1]=t[j-k. j-1]//and because t[j] = = T[k], there are t[0: K]=t[j-k.. j], launch "Next[j+1]=k+1"//and because next[j] = k, so "j++,k++"//"Conclusion: if NEXT[0]...NEXT[J] is known, and t[j] = = T[k], it can be next[j+1]=next[j]+1"NEXT[++J] = + +K; } Else { //******************* Recursive formula derivation *******************//because T[j] <> t[k], kee next[j] = k, you can launch t[0: k-1]=t[j-k: j-1]//because t[0: K-1]=t[j-k. j-1], there are t[y: K-1]=t[j-k+y: j-1] (0<=y<=k-1)//Next[k] = k, then there is t[0. K-1]=t[k-k. K-1], this time "Y=k-k"//there is the equation "T[y ... k-1]=t[j-k+y. J-1]=t[k-k ... k-1]=t[y: k-1]=t[0. K-1] "//that is t[j-k: j-1]=t[0. K-1], "next[j]=k=next[k]"//if Next time t[j] = = T[k '] = t[k], due to t[j-k: j-1]=t[0. K-1] and K=next[k], can be t[j-k: j]=t[0. K], i.e. next[j+1]=next[k]+1//"Conclusion: if NEXT[0]...NEXT[J] is known, and T[j] <> t[k], if t[j] = = t[k] = = T[next[k]], then there is next[j+1]=next[k]+1"//here to determine t[j] = = T[k '] is set up, so K=next[k]K =Next[k]; } }}//Print Next ArrayvoidPrint_next_array (intNext[],intlength) { for(inti =0; i < length; i++) {cout<< Next[i] <<' '; } cout<<Endl;}//KMP FindintKMP (CharS[],intS_len,CharT[],intT_len,intstart) { if(Start >= S_len)return-1; int*next =New int[T_len];//T string length is next array length if(S_len = =0)Throw "The original string cannot be empty! "; if(T_len = =0)Throw "Match string cannot be empty! "; if(T_len > S_len)Throw "The original string is shorter than the matching string, and the match (replace) fails! "; Calu_next_array (t, Next, T_len); //Find Next ArrayPrint_next_array (Next, T_len); inti = start, j =0; while(I < S_len && J <T_len) { if(j = =-1|| S[i] = =T[j]) {i++; J + +; } Else{J=Next[j]; } } Delete[] Next; if(j = = T_len)//T traverse to the end { returnIJ; } return-1;}//ReplacevoidReplace (CharS[],CharT[],Charr[]) { intS_len =strlen (s); intT_len =strlen (t); intR_len =strlen (R); if(S_len + r_len-t_len >= buf_size_2)Throw "Not enough buffers! "; intindex =0;//Start Index intLocation =0;//Match Location intCount =0;//Number of replacements intDelta = t_len-R_len; intRepl_min_len = (T_len <= r_len)?T_len:r_len; while(true) {Index=KMP (S, S_len, T, T_len, index); if(Index = =-1) Break; Location= index + Delta * count++; cout<<"find a matching location:"<< location <<Endl; //Replace part first { for(inti =0; i < Repl_min_len; i++) {S[index+ i] =R[i]; } } if(T_len >=R_len) { //has been replaced, start moving left Char*SRC = &s[index +T_len]; Char*dest = &s[index +Repl_min_len]; while(*src! =0&& *dest! =0) *dest++ = *src++; *dest =0; } Else { //move right, then replace the remaining Char*NULL_SRC = &s[index + Repl_min_len-1]; Char*SRC = &S[s_len]; Char*dest = &s[s_len + R_len-T_len]; while(src! = null_src) *dest--= *src--; Dest= src +1; SRC= &R[t_len]; while(*src! =0) *dest++ = *src++; } S_len-=Delta; Index-=Delta; Index++; } cout<<"Successful Replacement"<< Count <<"Place"<<Endl; cout<<"Replacement Result:"<< s << Endl <<Endl;}intMainintargcChar*argv[]) { Charbuf_string[buf_size_2]; CharBuf_find[buf_size]; CharBuf_replace[buf_size]; cout<<"Please enter a string:"<<Endl; Cin.getline (buf_string, buf_size_2-1); Cin.sync (); cout<<"Please enter a string to be replaced:"<<Endl; Cin.getline (Buf_find, Buf_size-1); Cin.sync (); cout<<"Please enter the replaced string:"<<Endl; Cin.getline (Buf_replace, Buf_size-1); Cin.sync (); Try{Replace (buf_string, Buf_find, buf_replace); } Catch(Const Char*pstr) {Cerr<<"****** error:"<< pstr << Endl <<Endl; } cout<<Endl; return 0;}
string and pattern matching (a)--KMP algorithm