Lyndon decomposition and minimum cycle learns

Make two points CF594E knowledge involved. The following notation string in Python.

Lyndon decomposition

$ S $ is defined Lyndon string, if and only if for any meaningful positive integer with a $ I $ $ S <S [i:] $.

Lyndon exploded defined $ S $ string is a sequence Lyndon $ s_1, s_2, \ ldots, s_n $, such that $ S = s_1s_2 \ cdots s_n $ and $ s_1 \ ge s_2 \ ge \ cdots \ ge s_n $.

Lyndon decomposition of the existence and uniqueness.

Difficult to find, so to obtain Lyndon decomposition: for $ S $, taking the smallest suffix $ S [i:] $, Lyndon decomposition $ S $ is the $ S [: i] $ appended to the end of the decomposition Lyndon $ sequence is obtained: a S [i] $.

Thus, Lyndon decomposition may find use suffix array, but this is too complicated.

Duval Lyndon decomposition algorithm:

Provided the original string is added by one character $ $ S, it has been able to determine the set $ S. [: I] $ Lyndon decomposition of $ s_1, s_2, \ ldots, s_n $ Lyndon decomposition of S $ $ prefix, and then as far as possible to expand the string $ S [i: k] $, such that $ S [i: k] $ Lyndon period has $ t $, say, $ S [i: k] = (n \ times t) u $, wherein U $ $ a (possible empty) prefix $ t $, and $ t $ string is Lyndon. Consider adding character $ S_k $.

  • If $ S_k <S_ {k- | t |} $, the $ S [i: k + 1] $ Lyndon period of no. But this time $ S [: i + n | t |] $ of Lyndon decomposition has been determined, i.e. $ S: Add n-$ $ a $ t $ [i] $, based on the new parameters to take $ i '= i + n | t |, k '= i' + 1 $ again.
  • If $ S_k = S_ {k- | t |} $, is the $ t $ $ S [i: k + 1] $ is Lyndon period.
  • If $ S_k> S_ {k- | t |} $, the $ S [i: k + 1] $ Lyndon cycle of its own.

We only realize the need to maintain $ i, k $ and $ j = k- | t | $ . Code link

Expansion $ S [i: k] $ during $ i + k \ le2 | S | $ and with the expansion of the total number of rounds is incremented, so the algorithm time $ O (| S |) $, removing input and output need only $ O (1) $ extra space, a very short and efficient algorithms.

Minimum Cycle means

Represents a minimum cycle, i.e. for the string S $ $, determined minimum $ S_i = S [i:] S [: i] $.

Minimum cycle based on the comparison may be expressed requirements.

Specifically, we maintain two decisions $ i, j $, to meet the $ i <j $ and $ [0, i) $ and $ (i, j) $ integer of not optimal decision.

Initially, set $ i = 0, j = 1 $.

We compare and find $ S_i $ $ S_j $ enumerate through the longest common prefix $ k $.

  • If $ S_i = S_j $, $ S_i $ is the optimal solution. This is because the string $ ji $ loop to loop section, while $ (i, j) $ is any integer not optimal decision.
  • If $ S_i <S_j $, description $ [j, j + k] $ integer in the minimum cycle is not indicated because it is better than $ [i, i + k] corresponding to the decision in the $ inferior, and therefore makes $ j \ gets j + k + 1 $. In this case the above-mentioned properties are not changed.
  • If $ S_i> S_j $, empathy so $ i \ gets i + k + 1 $, if $ i \ ge j $ then let $ j \ gets i + 1 $, the above-described properties remains unchanged.
When the $ j \ ge | S | $, we found $ [0, i) $ and $ (i, | S | integer) $ is the smallest decisions can not, so $ S_i $ is the only best solution; otherwise, repeat the process.
$ I + j + k \ le 3 | S | $ enumeration with the total number of increments, so the algorithm time $ O (| S |) $, O removed only $ O (1) $ extra space, is a very short and efficient algorithms.

Guess you like

Origin www.cnblogs.com/nealchen/p/12002895.html