Failure function (ZT) in the KMP matching algorithm

xiaoxiao2021-03-06  98

Today, I finally understand the KMP in the string of the character string, I will write down, it is convenient to check it later.

Failure function: set mode P = P

0P

1 .... p

M-2P

M-1, then its fail function is defined as follows:

f (j) = k | When 0 <= k

J-KP

J-K 1 ... P

maximum number of J

f (j) = -1 | Other situations.

J01234567Pabaabcacf (j) -1-1001-10-1 is detailed, remember the algorithm.

Void

String :: fail

{INT lengthp = curlen; f [= 1; j = 0) i = f [i]; // Retroflar calculation IF (* (CH J) == * (CH I 1)) F [J] = i 1 ; Elsef [j] = - 1;}}

Here is a normal matching algorithm:

int

String :: Find (String

&

PAT)

Const

{Char * p = pat.ch; * s = ch; int i = 0; if (* p && * s) while (i <= curlen-pat.curlen) if (* p == * s ) {// C classics if (! * P) return i;} else {i ; s = CH i; p = pat.ch;} return -1;}

Here is the KMP algorithm:

int

String :: fastfind (String)

&

PAT)

Const

{INT POSP = 0, PAST = 0; intlengthp = pat.curlen, lengtht = curlen; while (POSP

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

New Post(0)