KMP Algorithm---Fast solution to next array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.