Recently the KMP algorithm curiosity, this algorithm completely no impression (school taught, but I did not learn, because skipping class), also not returned to the teacher. Just want to jump job, heard all have to understand this thing.
On the Internet to find an article, this buddy should be considered more famous, http://www.matrix67.com/blog/archives/115.
Just beginning to see, a word "halo". What KMP,BF,BM, the head all look ache. Look carefully before you understand. The following talk about BF and KMP, no code, try to explain the popular point.
BF does not elaborate, this is quite "popular" and natural (I describe myself as the original idea), the idea of comparing strings from beginning to end, not equal to the beginning. BF Portal
Of course, you have to understand the BF algorithm. KMP is on the basis of BF, will each time from the beginning to compare this operation to optimize, let this operation do not start from scratch, according to the previous comparison (these operations not only consumes the CPU, there are other uses, can not be wasted), to optimize the contrast offset. Of course, there are the worst cases, and we have to start all over again. But this is not the case in most of the reality.
Mention here, lest the interview to ask, KMP algorithm complexity of O (m+n).
Or give an example:
A string: A B A b d a B c e h a B c D
B String: A B a b d (e)
1 2 3) 4 5
This example is compared at one time, but after the last character d of the B string is changed to E, after comparing the D in B string E with a string, the BF will compare directly from the beginning of a. And KMP will not, is from the second B after the comparison, that is, a (subscript 3) and a string of a B (subscript 5), if the equality is compared to the next character B string, not equal to the beginning of the comparison, but a string has been backward contrast.
Why is this, in the words of the previous comparison has been proved no need to compare from the beginning (seemingly nonsense). Or draw a picture, no picture, no truth.
A+b = C + D, and a = B, C = d, when there is a case where E and F are unequal, will you once again start comparing the second element of the string with the first element of the string below?
No. You're sure to do the next comparison.
That is, the part of C that has been confirmed to be equal to B is considered equal, only the part of D with the E and the following part of the next comparison, here to see clearly, write the next comparison. The KMP is then performed several times, until the above string is compared. The sign of success in comparison is the following series of comparisons.
Does this save a lot of extra contrast?
Drawing can be drawn in this way, and the writing program will have the option to initialize the next point of contrast. The illustration is just the main idea, please refer to the link at the beginning of the article.
Work most like to draw such a picture or write text, rather than the programmer usually draw the flowchart, feel that things are not very consistent with people's thinking patterns, of course, it may be just mine.
Reprint please indicate the source, thank you.
KMP Algorithm Popular explanation