I will not talk about the principle. All the algorithm learners know that this is basically an algorithm for finding whether a string is positioned in another string faster. The Code is as follows: [cpp] # include <stdio. h> # include <string. h> # define MAXSIZE 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 position % d of source string", KMP (); return 0 ;}