If you want to reprint, please keep this article link.
Jake Boxer English blog post link: http://jakeboxer.com/blog/2009/12/13/the-knuth-morris-pratt-algorithm-in-my-own-words/
How to use partial match tables
1. Function: Use partial match table to skip the old comparison part that has already been done, starting from the non-repeating match part.
2.if (match table result greater than 1)
{
Number of characters allowed to skip = match Length-partial match table [match length-1];
}
Else
{
Number of characters allowed to skip = partial match table [match length-1];
}
To illustrate:
Use "ABABABCA" as a substring to match the "Bacbababaabcbab" string.
Some of the matching tables are as follows:
String A|b|a|b|a|b|c|a
Index value 0|1|2|3|4|5|6|7
Match value 0|0|1|2|3|4|0|1
First time match
Bacbababaabcbab
Abababca
Result: No matching results
Second match
Bacbababaabcbab
Abababca
Result: The number of matches is: 1
Number of characters allowed to skip = partial match table [1-1] = 0;
Fifth time Matching
Bacb|ababa|abcbab
|ababa|bca
Result: The number of matches is: 5
Number of characters allowed to skip = 5-Partial match table [5-1] = 5-3 = 2;
Sixth time Matching
Bacbab|aba|abcbab
Xx|aba|babca
After skipping two characters, the match result is as above, this match result is: 3
Number of characters allowed to skip = 3-Partial match table [3-1] = 3-1 = 2;
Seventh time Matching
Bacbabab|a|abcbab
Xx|a|bababca
After skipping two characters, the match result is as above, this match result is: 1
At this point, the tail character of the substring has exceeded the length of the main string, so there is no matching result between the main string and the substring.
The next article is the KMP algorithm analysis of the big liar data structure.
KMP algorithm Learning record----Jake Boxer Blog Learning Section