1.1 KMP needed to solve the problem
KMP, is the sum of three names. The main problem is the string matching problem. That is to give a certain length of strings and a text, asking you to find the string in the text where the first occurrence, and then the string and text length may be very large.
As such a question: POJ-3461
Test instructions is probably to find out the number of occurrences of the first string in the second string.
1.2 Traditional brute force algorithms
The brute force algorithm is to search by brute force, starting with the first letter. If it does not match, then move the entire string back one bit. The algorithm (on^2) obviously becomes unbearable in some cases. But we can find out by understanding The main reason for the slowness of this algorithm is that when a letter mismatch is found, it is only possible to move back one bit. Then KMP is the way to optimize this backward jump. To achieve the goal of optimizing time .
The specific operation process of 1.3 KMP algorithm
Let's not talk about the principle of this algorithm, because I am the first to know the operation process, and then the general understanding.
1.3.1 Longest prefix string
We introduce a concept, the longest prefix string
Some strings like this:
Abccsab its longest prefix string is AB
ABSSC It does not have the longest prefix string
AAAAA its longest prefix string is AAAA
1.3.2 Next Array
This is the first step we need to take. Divide this string into
Basic operation and self-understanding of the KMP algorithm