In the KMP algorithm, the key is to solve the next array. So how to quickly solve the next array?
Known pattern string: A b C d ab d D A
Its next array: 0 0 0 0 12 0 0 1
So how to prove it?
The string is first traversed from left to right.
The next array of the first character a corresponds to the element 0,
The first character a and the 2nd character B are not equal. b:0 (the corresponding element of the next array representing character B is 0);
The first character a and the 3rd character, C, are not equal. c:0
The first character A and the 4th character, d, are not equal. d:0
The first character A and the 5th character A are equal. A:1
The second character B and the 6th character B are equal. B:2 (accumulated on the result of the previous character) (after equality, the active character (the character that will first be compared to the next character is called the active character, the subsequent character is called the passive character), and a bit backward, B. The passive character will still move backward one bit)
Third character C and 7th character D ratio, unequal (at this point the active character immediately switches to the first character a)
The first character a and the 7th character, D, are not equal. d:0
The first character a and the 8th character, d, are not equal. d:0
The first character A and the 9th character A are equal. A:1
And so on
Don't look so troublesome, everyone in the paper pushed over the clear at a glance.
KMP Algorithm---Fast solution to next array