KMP algorithm for cycling festival, why can seek circulation section

As we all know, KMP algorithm can seek a minimum cycle section, you can ask why the cycle section of it?

Before bloggers understanding of the KMP algorithm is not deep enough, the recent sudden afterthought, a bit-depth study.

 KMP algorithm is the next most important arrays, next [i] indicates the maximum value of the length of the string and the prefix string matches all i in sub-terminated non-prefix. May be a bit around, thread a ray of it, this is the next [] array definition only.

We seek a minimum cycle festival also use next [] array of requirements. Suppose string is a string loop, there can be many times a cycle loop section (assuming more than one), may assume a circular section str1 , original character string must be derived from the circulation loop section, i.e., string = str1str1. ..str1 , if the string length len, according to [the definition of next] array, next [len] is to the end of the string to the end of the maximum length of non-matching prefix length, if the string can be recycled by one loop section many times, then the next [n] comprises at least a certain cycle section, and the whole string is removed from the beginning of the first cycle of the postganglionic. Painted himself look like in the book. FIG alignment certainly is like this, each block is a circular section.

Because we set a minimum cycle section, if think there are other ways, then it must be aligned when the second string to move forward, if this is the case then it is certainly not the smallest circulation section. In this case, the minimum length of the section is circular len - Next [len] , if it can repeatedly come from the circulation, must meet the following conditions are satisfied.

% only ( referred to as - next [only] ) == 0

If len - next [len] == len tell the whole string is a circular section, special sentence just fine. Of course, this required minimum cycle section, sub-section should be small circulation len - the Next [the Next [len]] , and so, of course, to determine what it needs.

Published 204 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/104540823