KMP minimum cycle

Source: Internet
Author: User

KMP minimum cycle and cycle:

Theorem: if the length of S is Len, S has the smallest cyclic section. The length of the cyclic section is len-next [Len], and the substring is s [0... Len-next [Len]-1].

(1) If Len can be divisible by Len-next [Len], it indicates that string s can be completely composed of cyclic nodes, with the cycle T = Len/L.

(2) If not, you must add a few more letters to complete. The number of loops to be supplemented is L-len % L = L-(LEN-l) % L = L-next [Len] % L, L = len-next [Len].

 

 

To understand this theorem, we first need to understand the meaning of the next array: Next [I] indicates the maximum length of the substring with the same prefix and suffix as that of I.

For example, abcdabc

Index

0

1

2

3

4

5

6

7

Char

A

B

C

D

A

B

C

 

Next

-1

0

0

0

0

1

2

3

 

For example, for a, AB, ABC, ABCD, it is obvious that the length of the prefix and suffix is 0.

For a 5-character sub-string abcda, the Prefix A is the same as the suffix A, and the length is 1

For a 6-character substring abcdab, the prefix AB is the same as the suffix AB, And the length is 2

The following example shows the minimum cycle and cycle:

For convenience, first set the length of the string to Len, and the length of the loop substring to L

1.

S0s1s2s3s4s5, next [6] = 3

That is, s0s1s2 = s3s4s5

It is obvious that the sub-string of the loop is s0s1s2, L = len-next [6] = 3, and can be divisible by Len.

 

2.

S0s1s2s3s4s5s6s7, next [8] = 6

In this case, len-next [8] = 2, that is, L = 2

By s0s1s2s3s4s5 = s2s3s4s5s6s7

We can see that s0s1 = s2s3, s2s3 = s4s5, s4s5 = s6s7

Obviously, s0s1 is a circular substring.

 

3.

S0s1s2s3s4s5s6, next [7] = 4

In this case, len-next [7] = 3, that is, L = 3

By s0s1s2s3 = s3s4s5s6

We can see that s0s1 = s3s4, s2s3 = s5s6

So we can see that s0s1s2 = s3s4s5, S0 = S3 = S6

That is, if 3-4% 3 = 2 letters (s1s2) are added, the resulting string can be made up of s0s1s2 loops three times.

 

This theorem can be understood as follows:

Http://www.cnblogs.com/oyking/p/3536817.html

For a string, such as ABCD, which is obtained three times by repeating the 4-character string ABCD, the first eight digits of the original string must be equal to the last eight digits.

That is to say, for a string of S, the length is Len, and the string s with the length of L is repeated for R times. When R is ≥2, s [0 .. len-L-1] = s [L .. len-1], string subscript starts from 0

Next [Len] = len-L is available for the KMP algorithm. At this time, l must be the smallest (because the value of next is the maximum length equal to the prefix and suffix, that is, len-L is the largest, then when len has determined, l is the smallest ).

 

If you must prove it carefully, please refer to the following:

(For details, refer to http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html, which has been modified)

K m x j I

From top, next [I] = J, the two red strings are equal (the two strings are completely equal), s [k .... j] = s [m .... i]

Set S [X... J] = s [j... I] (XJ = Ji)

The following is a string expression.

KJ = kx + XJ;

Mi = MJ + JI;

Because XJ = Ji, kx = MJ, as shown in

K m a x j I

Set S [... X] = s [X. J] (AX = XJ)

We can see from XJ = Ji that AX = XJ = Ji

That is, s [... I] is composed of S [... X] obtained after three cycles.

And I can't see it. Now I have repeated the above model, s [K... X] = s [M... J]. It can be recursive all the time.

Finally, we can introduce the theorem mentioned at the beginning of the article.

 

Finally, we will give two examples.

Abdabdab Len: 8 next [8]: 5

Minimum Cycle Length: 3 (that is, Abd) the number to be supplemented is 1 D.

Ababa Len: 5 next [5]: 3

Minimum Cycle Length: 2 (that is, AB) the number to be supplemented is 1 B

--------------------- This article is from the csdn blog of hao_zong_yin. For the full-text address, click: 77455285? Utm_source = copy

KMP minimum cycle

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.