I wrote KMP algorithm

zhaozj2021-02-16  54

#include #include #include using namespace std; void init (string, string); void show (char [], int); int KMP (String, String, INT POS); Void get_next (char *, int *); String S1, T1; INT M, N; Char * S; char * t; int * next; / **************** ************************************************************************************************************ / INT Main (int Argc [], char * args []) {double t = clock (); cout << "Found location:" "" "" "" "" "" "" "" aABCA ", 1) << Endl; delete [] S; delete [] next; cout << "time-consuming:" << () - t) / 1000 << "milliseconds!" << Endl; return 0;} / ******** ************* Next ************************************************** / void get_next (char s [], int ne = new int [n 1]; Next = new int [n 1]; Ne [0] = 9999; INT i (1), J ( 0); NE [1] = 0; While (i <= (int)) // array is a character type, to convert to an integer {IF (j == 0 || T [i] == T [j]) { i; j; ne [i] = j;} else j = ne [j];} for (i = 1; i <= n; i ) {cout << "Next [" << i << "] =" << Ne [i] << endl; next [i] = ne [i];}} / ************* ****** KMP ***************************************************** / INT KMP (String) S0, STRING T0, INT POS) { Init (S0, T0); INT i = POS, J = 1; While (I <= (INT) (S [0])) && (j <= (int))))))) {IF ((j == 0) || (s [i] == t [j])) { i; j;} else j = next [j];} if (j> (int) (T [0])) Return I - ((int) (T [0])); ELSE RETURN 0;

转载请注明原文地址:https://www.9cbs.com/read-22274.html

New Post(0)