Next array understanding of kmp algorithm (fast substring matching)

Because there is not much time, this article just hurriedly described the feelings at this time

Understanding of the next array:

That is, the next array is equivalent to a conversion function. When a certain position does not match, find the position corresponding to the next array, and see how to slide the substring.

Because you don’t need to look at the main string at all, just look at the substring, so the next array records the current mismatched configuration, which character position before the current mismatch position of the substring slides over (because the substring is going back , That is, how much to stop)

eg:

Substring s[]="0abab e opr" (assuming that the initial subscript is 1 to start recording the substring), assuming that there is a mismatch at s[5], that is, the first four (abab) are all matched, then a match should be found The longest common suffix of the character segment (abab) (the concept needs to be clarified) is easy to know as ab, so the letter in the position after the longest common suffix (ie the second a) slides to e, also That is to say next[5]=3 (3 is the subscript of the second a) at this time.

Assuming that there is no match at s[1], and we stop at the beginning, then next[1]=0, that is, let s[0] slide to here, that is, the whole substring moves back one position.

As for why it is possible to slide multiple characters directly like this, instead of simply going back to the original place when it encounters a mismatch like the BF algorithm, it is not difficult to understand, like the above, the longest common suffix ab, The front and back must be equal, that is, the first n still match after sliding like this (n=longest common prefix and suffix length)

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/108143420