kmp algorithm notes (easy to understand)

Usually a short string string string m long is n, then the complexity of the method is to use violence O (m * n)

But it can reach kmp O (m + n)! ! ! ! ! !

For this magical algorithm, I am also half-comprehended,

Below is a simple method of seeking kmp

1, find the next array

  This kmp an array of soul! next to an array of short tandem seeking n

  step:

    1)next[0]=-1

    2) next [i] = string preceding the maximum length of a common substring

 

    example:

    Set m - is: ababc, string length is 5, then the next length of the array 5, corresponding to the following next array

    

       You can see, b following a 1, this one is how come it? "" The foregoing is the string come aba, the maximum length of a common substring of a string aba

    aba maximal common substring of a, from the left as the maximum consecutive substring (less than the letter string length) is a, the maximum number of successive substring from the right is a, so the next [3] = 1

                         

 

 

    c is below 2 since the maximum length of the preceding common substring of string abab 2 (common substring ab)

                    

 

 

 

    Another example aaaa maximal common substring of length 3 (from the left substring = Maximum continuous from the continuous string on the right = the maximum sub-AAA)

                    

 

 

2, use the next array, n short and long strings matching string m

  1, firstly the first three characters match, but does not match to the arrow, moves below for m -

  

 

  2, after moving below

  

     How to move it?

     Since a and b are so matched string m to the right, see the array b next below corresponds to a value 1, then m is the string index for the location and alignment of the arrows.

     Continuing now rearwardly from Comparative arrow, no match is found, then the mobile "" see that the value corresponding to the B 0, then the string subscript m for the location and its arrow 0

    

 

     Still do not match, then move, this time a subscript corresponding to -1, -1 position but without the array, in fact, the position where the arrow 1 in its right is equivalent to a one-dimensional string m, attention here to more an operation done, the comparison is a rearward position, where n if the string is marked arrow j, then execution j ++, opposite m - performing i ++, following FIG.

    

 

  Comparison continues

    

  Can be seen, the end of the arrow m went string, to match! !

  3, can be seen to match, if m and there are sub-string will then continue to look for mobile subscript m string positions such that its arrow 2, the end does not match the query.

  

 

Here are just introduce principles and methods, there are many online do not show the code, here is a video tutorial on the beep beep miles miles, if not understand can look at the video connector attached ( https://www.bilibili.com/video/ av52365939 /? spm_id_from = trigger_reload )

 

Guess you like

Origin www.cnblogs.com/51python/p/11518089.html