KMP Algorithm and Its Python code example.
The KMP algorithm is a classic string matching algorithm that solves the problem of searching the string M from string S. The algorithm name comes from the inventor Knuth, Morris, and Pratt.
It is assumed that the length of M, S is ls, and M is lm from the string S, and (ls> lm ).
Simple string SEARCH method
Start from the first character of string S and compare it with M. If the matching fails. Start from the next character and compare again. Guides the characters (ls-lm.
This method is easy to think of, easy to understand, and inefficient.
The problem is that after each matching failure, the moving pace is fixed to 1, in fact, the step can be larger.
KMP string SEARCH method
Assume that M [0, I] And I <lm of the continuous string in the mode string have successfully matched string S. But unfortunately the I + 1 character fails. What should I do? Move a character and try again with a new one? Of course it is not good, that is, the simple route. Can we continue from where we fell?
Since the string M [0-I] has been matched successfully, let's start with this substring. Example
S no.
|
J
|
J + 1
|
J + 2
|
J + 3
|
J + 4
|
J + 5
|
J + 6
|
J + 7
|
...
|
S string
|
A
|
B
|
C
|
A
|
B
|
C
|
D
|
E
|
...
|
M string
|
A
|
B
|
C
|
A
|
B
|
D
|
|
|
|
|
M no. |
0
|
1
|
2
|
3
|
4
|
5
|
|
|
|
At this time, the matching fails to be 5th characters in the M string, and the first four characters have been matched successfully.
If you start from the fall point, the Child string M [0, 4] must exist. M [0, k] = S [j + 4-k, j + 4].
Because M [0, 4] = S [j, j + 4] has strings S [j + 4-k, j + 4] = M [4-k, 4]. In summary, M [0, k] = M [4-k, 4]
If such k does not exist, it will be honestly simple.
From the table above, we can intuitively see that the next matching operation only needs to move the M string to the j + 3 position and start matching from j + 5. It is easy to see that M [0, 4] has the longest substring (M [0, 1] = M [3, 4]), this is the key to the problem.
Therefore, the core of KMP is the k of each substring of the computing mode string.
Instance
First, let's take a look at the simple matching of strings.
It can be imagined that the text string s is fixed, and the pattern string p is aligned from the left of s. If the alignment part is identical, the matching is successful, if it fails, the entire p of the mode string is shifted to the right and the alignment is checked again and again.
# Simple match def naive_match (s, p): m = len (s); n = len (p) for I in range (m-n + 1 ): # Start pointer I if s [I: I + n] = p: return True return False
For more information about the kmp algorithm, see <string matching KMP algorithm>.
In fact, it is used to pre-process the pattern string p and obtain the matching table with the prefix and suffix, so that we can use known information to calculate the number of digits that can be shifted to the right. that is, kmp = simple match + mobile Multibit.
For more details, see Ruan Yifeng's article. I will not start it here.
The python code implementation is given below.
# KMP def kmp_match (s, p): m = len (s); n = len (p) cur = 0 # Starting pointer cur table = partial_table (p) while cur <= m-n: for I in range (n): if s [I + cur]! = P [I]: cur + = max (I-table [I-1], 1) # With some matching tables, we don't just move one bit and one bit to the right, you can move the multiple-digit break else at a time: return True return False # partially match the table def partial_table (p): ''' partial _ table ("ABCDABD")-> [0, 0, 0, 0, 1, 2, 0] ''' prefix = set () postfix = set () ret = [0] for I in range (1, len (p): prefix. add (p [: I]) postfix = {p [j: I + 1] for j in range (1, I + 1)} ret. append (len (prefix & postfix or {''}). pop () return ret print naive_match ("bbc abcdab abcdabde", "ABCDABD") print partial_table ("ABCDABD") print kmp_match ("bbc abcdab regular", "ABCDABD ")