I will not talk about the principle.AlgorithmAll know that this is basically an algorithm to find whether a string is positioned in another string faster.CodeAs follows:
[CPP] # Include <stdio. h> # Include <string. h> # Define Max size 100 Int next [maxsize]; Int s_lenth, d_lenth; Char source [maxsize], detination [100]; Void get_next () { Int I = 1, j = 0; Next [1] = 0; While (I <= d_lenth) { If (j = 0 | (detination [I-1] = detination [J-1]) { ++ I; ++ J; Next [I] = J; } Else { J = next [J]; } } } Int KMP () { Int I = 0, j = 1; While (I <= s_lenth & J <= d_lenth) { If (j = 0 | (source [I] = detination [J-1]) { ++ I; ++ J; } Else { J = next [J]; } } If (j> d_lenth) { Return i-D_lenth; } Else Return 0; } Int main () { Int I = 0; Printf ("Input Source string !!! \ N "); Scanf ("% s", & source ); Printf ("input mode string !!! \ N "); Scanf ("% s", & detination ); S_lenth = strlen (source ); D_lenth = strlen (detination ); Get_next (); Printf ("next array element: \ n "); For (I = 1; I <= d_lenth; I ++) { Printf ("% d", next [I]); } Printf ("\ n mode string starting at the % d position of the source string", KMP ()); Return 0; }