KMP algorithm to find next array

Source: Internet
Author: User

Next The solution for the array is: The next value of the first bit is 0, and the next value of the second bit is 1. When the next value of each bit is solved, the comparison is made according to the previous one. First compares the previous bit to its next value, and if it is equal, the next value of the bit is the next value of the previous bit plus 1; if not, go ahead and look for the next value to compare to the previous one. Until the next value corresponding to the contents of a bit is found to be equal to the previous one, then the value of this bit corresponds to the next value of 1, and if the first bit is found to be equal to the previous one, then the next value on the required bit is 1.


NextvalThe solution for the array is: nextval[1]=0. Starting from the second position, if nextval[i] is required, the value of the bit corresponding to the value of next[i] is compared with the value of I (for example, the value of I is ' B ', next[i]=3, the value of I ' B ' is compared with the value of the third bit), if equal, nextval[i]= Nextval "Next[i]" (example, nextval[i]=nextval[3]); If not equal, then nextval[i]=next[i] (example, nextval[i]=next[i]=3).

Pattern string

A

B

A

A

B

C

A

C

Next value

0

1

1

2

2

3

1

2

Nextval value

0

1

0

2

1

3

0

2

1. The Nextval value of the first bit must be 0, and the second bit is 0 if it is the same as the first digit, or 1 if it is different. 2. The next value of the third bit is 1, then the third and first digits are compared, both a and the same, then the third bit of the Nextval value is 0. 3. The next value of the fourth bit is 2, then the fourth and second digits are compared, and the fourth bit Nextval value is its next value, which is 2. 4. The next value of the fifth bit is 2, then the fifth and second bits are compared, the second bit is the next value is 1, then the second bit continues to be compared with the first bit, and the fifth bit Nextval value is the next value of the second bit, which is 1. 5. The next value of the sixth bit is 3, then the sixth and third digits are compared, and the sixth bit's Nextval value is its next value, which is 3. 6. The next value of the seventh bit is 1, then the seventh bit is compared with the first bit, and the Nextval value of the seventh bit is 0. 7. The next value of the eighth bit is 2, then the eighth and second digits are compared, and the eighth bit Nextval value is its next value, which is 2.

Validation can be performed within "Aaaab":

Pattern string

A

A

A

A

B

Next value

0

1

2

3

4

Nextval value

0

0

0

0

4

KMP algorithm to find 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.