The KMP algorithm is used to search for a word with a length of m in a document with N letters;
Brute-force enumeration: Compare the first M letters and words in the document, and then compare them with 2nd to m + 1, and then 3rd to m + 2; in this way, the complexity of the algorithm reaches O (M * n) at the worst, which is certainly not good for big data.
The essence of the KMP algorithm is to try to reduce unnecessary enumerations, for example; for example, the first K-1 letter that has matched the word; but the k letter cannot match; so if there is a situation similar to the background in the first K-1 letters (the substring consisting of the first I letter is the same as the substring consisting of the last I letter ), then the pointer J is changed to I (which is equivalent to moving the whole to the right) to reduce the number of enumerations. Therefore, you can pre-process the words to be searched, next [I] = t to save the word in front of the I-1 letters, the first t letters of the Child string and the last t letters of the Child string is the same; and T as big as possible;
How to find next [I? Initialize next [0] = next [1] = 0; before and after recurrence; Let J = next [I-1 ]; indicates that the first J of the first I-2 letter is the same as the last J; then if s [J] (J + 1) = s [I-1] (I letter), next [I] is equal to next [J] + 1; if not equal, then J into next [J ];
After reading two evening lessons, I can see it clearly .. It is really hard to understand...
The following is a small application: (poj2752, yzoi1780)
Given a string s with a length less than 400000, we need to find all I. The substring consisting of the first I letters that meet the requirements of S is the same as the string consisting of the last I letters;
All I incremental output is required;
For this question, you only need to find the next array. Be sure to find one more bit (calculated to the next [Len] position); j = Len; next [J] = T indicates that the first t letter of S is the same as the last t letter; then the second largest I (the maximum I is the length of the string) is obtained ); then, every cycle j = next [J]; The value of next [J] is an I value (remove 0 );