Table of content
Teleporter | Description |
---|---|
I. The Problem | Describe about the problem |
II. Bruteforce Approach | Simple way to do the problem |
III. Sqrt Decomposition | Divide into parts, solve each part then solve the whole part |
IV. Hashing Approach | Reduce circular substring comparing complexity |
V. KMP Approach | Magical Linear Booth-KMP Algorithm |
VI. Lyndon Factorization Approach | Incredible Linear Duval-Lyndon Algorithm |
VII. Suffix Array Approach | On Progress |
VIII. Suffix Automation Approach | On Progress |
IX. Elimination Tournament Algorithm | From initial candidates, eliminate worst candidates until find one final champion |
................................................................... | .......................................................................................................................... |
I. The Problem
A) Statement:
Sometimes being as a coder, you will find some real life problems about strings. Some strings are just simple an array of characters with 2 ended. Sometimes you will face with circular strings, the one which circular around. Let take you to the biological laboratory, dont worry, I just teleport you without requiring any money. In the lab, you will find many interesting bacteria. How could you detect which one is belonged to the same group or not ? Let simplify the problem that two bacteria are in the same group if their circular encoded-DNA strings are the same. So how can you compare two randomly encoded-DNA ? One of the effectively way to do that is convert all strings into Minimal Lexicographical Acyclic Strings (a.k.a Lexicographically Minimal String Rotation) and then hash them for faster comparison.
This bring down to a much simpler problem. Let define a right-rotation of a string is that putting the leftmost character to the rightmost position. Given circular DNA string $$$S$$$ of size $$$n$$$. Find the minimum right-rotation to make it Minimal Lexicographical for all such rotations.
B) Question:
Given a string of latin words $$$S$$$ of size $$$N$$$ ($$$N \leq 10^5$$$)
A right-rotation of $$$S$$$ is that $$$S_2 + S_3 + ... + S_{|S|} + S_1$$$ where ('+' notation mean concatenation)
Find the least right-rotation to make $$$S$$$ become the Lexicographically Minimal
D) Practice Links
II. Bruteforce Apprach
A) Sorting and Searching:
For every possible rotation of the string, add it into map with its number of rotations needed
The answer will be the smallest string (minimal lexicographical) in map with lowest index
B) Loop over all rotations:
Why should we store then sort anyway, just need to loop over all rotations and comparing to select the smaller
For convention, you can just duplicate the string
- You can also optimize it to break when first difference is detected
C) Substring Based Elimination
1) Ideas:
- We have a set of candidates, where each one might be the optimal starting point for lexicographically minimal string rotation. We eliminate the ones whose order is higher (higher than minimal)
First, let $$$mn$$$ / $$$mx$$$ is the minimal / maximal character of $$$s$$$
Define: $$$mn = \underset{c \in S}{min}(c)$$$ and $$$mx = \underset{c \in S}{max}(c)$$$
Pre-elimination Round: We take the position $$$p$$$ that $$$s[p] = mn$$$. Since all other positions wont provide Minimal Lexicographical Circular String
Define: $$$candidate = $$$ { $$$p\ \ |\ \ p \in $$$ { $$$0, 1, \dots, |s| - 1$$$ } $$$ \cap s[p] = mn$$$ }
Then we take maximumly $$$n - 1$$$ Rounds from $$$d = 1$$$ to $$$d = n - 1$$$. For all current candidate, add the next character to it. Find the minimal substring, and eliminater unsatisfied others.
Optimization: At the $$$p$$$ round we will find $$$c = $$$ minimal character among all candidate's $$$pth$$$-next character, then traverse again and eliminate the one whose $$$pth$$$-nxt character is greater than $$$c$$$
Define: $$$s[x] = s[x\ mod\ |s|]$$$ and $$$c = \underset{p \in\ candidate}{min}(s[p + d])$$$ and $$$next\ candidate = $$$ { $$$p\ \ |\ \ p \in candidate \cap s[p + d] = c$$$ }
2) Example:
- $$$S = $$$"$$$abaabaaabaababaaabaaababaab$$$"
3) Notice
- After all eliminations, if there are more than one candidate, select the one with lowest index
4) Complexity
If the string is $$$k$$$-repeated strings (each substring size $$$d$$$) or similar then there will be atleast $$$k$$$ candidate after $$$d$$$ eliminations. About $$$O(k \times d)$$$ in the best cases
Best case to say, like a string where all characters are unique, then it will be Linear $$$O(n)$$$
Worst case to say, like a string where all characters are the same, then it will be $$$O(n \times n) = O(n^2)$$$
5) Implementation
- For convention, let not use obviously-not-optimized code
III. Sqrt Decomposition
1) Divide candidate list into many parts
- Here is the simple algorithm
Let a candidate to say a starting position that might leed to lexicographically minimum string rotation. Hence we have the initial candidates are $$$0, 1, \dots, |S| - 1$$$
Let divide the candidates list into parts of size $$$\approx K$$$ (the final part might have much less). There will be $$$\lceil \frac{N}{K} \rceil$$$ parts.
Small iteration: For each parts, we find one smallest circular substring of size $$$K$$$, among all equal circular substrings, pick the one with smallest starting position. Each part will produce only one candidate
Big iteration: For $$$\lceil \frac{N}{K} \rceil$$$ next candidates we will find one smallest circular substring of size $$$N$$$, among all equal circular substrings, pick the one with smallest starting position. This will give you the answer
- Here is the proof:
--------------------------
Let $$$A, B$$$ are the circular substrings start from candidates positions, that $$$|A| = |B|$$$. And let $$$X, Y$$$ are the prefixes of $$$A, B$$$, that $$$|X| = |Y|$$$
Since we are comparing in lexicographical order, if $$$X < Y$$$ then it will lead to $$$A < B$$$. Hence by using small iteration we sieved all unoptimal candidates, and reduce $$$N$$$ candidates to $$$\lceil \frac{N}{K} \rceil$$$ candidates only.
- Here is the complexity:
For small iteration, there are $$$N$$$ candidates, and each candidate are compared with $$$K$$$ lengthed circular substrings. Hence $$$O(N \times K)$$$
For big iteration, there are $$$K$$$ candidates, and each candidate are compared with $$$N$$$ lengthed circular substrings. Hence $$$O(\lceil \frac{N}{K} \rceil \times N)$$$
The total time complexity is $$$O(N \times (K + \lceil \frac{N}{K} \rceil))$$$
For optimal purpose, let $$$K \approx \sqrt{N}$$$ the complexity therefore will be only $$$O(N \sqrt{N})$$$
- Here are the notices:
We are comparing string in circular, you can either use modulo position or duplicated string
Becareful that the real size of string and the duplicated one
- Here are the implementations:
For convention, let just ignore those obvious not a good code (ugly — complex — stupid code worth nothing to say)
2) Hash Improvement
By using hash for comparing two known substrings $$$X$$$, $$$Y$$$ of equal length $$$D$$$. We can compare them from $$$O(S) \rightarrow O(log(S))$$$ by finding their LCP — Longest Common Prefix and comparing $$$X[lcp]$$$ vs $$$Y[lcp]$$$
The total time complexity is $$$O(N \times log(K) + \lceil \frac{N}{K} \rceil \times log(N)) \approx O(N \times \sqrt{N})$$$ but significant smaller constant (About 2 or/to 3 times faster). But the trade off is more complex code and more space is taken.
Implementation:
IV. Hashing Apprach
1) Loop over all rotations
- Iterating over all rotations and comparing for finding the smaller
If they are fully equal, then that position not exceed, then we take the one which smaller index
Else find the first position of character that they are difference, which character is smaller, the which of it is also smaller
- Therefore we have these implementations:
- Hash are so heavy of hidden constant, obviously most by modulo operators, but you can have some tricks to solve it
Significant improvement: Declare $$$MOD,\ BASE,\ LIM$$$ as
const
orconstexpr
In Single Hash, you can use overflow modulo for significant faster but also dangerous in same cases
Replace vector with pre-declared array
Replace recursive power function by iterative one
Improve time to calculate inverse modulo. You can do it linear but cost more space and modulo operators, so it is better to do like below
2) Logarithm Decomposition
- Here is the simple algorithm
Let a candidate to say a starting position that might leed to lexicographically minimum string rotation. Hence we have the initial candidates are $$$0, 1, \dots, |S| - 1$$$
Let divide the candidates list into parts of size $$$\approx K$$$ (the final part might have much less). There will be $$$\lceil \frac{N}{K} \rceil$$$ parts.
Small iteration: For each parts, we find one smallest circular substring of size $$$K$$$, among all equal circular substrings, pick the one with smallest starting position. Each part will produce only one candidate
Big iteration: For $$$\lceil \frac{N}{K} \rceil$$$ next candidates we will find one smallest circular substring of size $$$N$$$, among all equal circular substrings, pick the one with smallest starting position. This will give you the answer
- Here is the proof:
Let $$$A, B$$$ are the circular substrings start from candidates positions, that $$$|A| = |B|$$$. And let $$$X, Y$$$ are the prefixes of $$$A, B$$$, that $$$|X| = |Y|$$$
Since we are comparing in lexicographical order, if $$$X < Y$$$ then it will lead to $$$A < B$$$. Hence by using small iteration we sieved all unoptimal candidates, and reduce $$$N$$$ candidates to $$$\lceil \frac{N}{K} \rceil$$$ candidates only.
- Here is the complexity:
For small iteration, there are $$$N$$$ candidates, and each candidate are compared with $$$K$$$ lengthed circular substrings using hash. Hence $$$O(N \times log(K))$$$
For big iteration, there are $$$K$$$ candidates, and each candidate are compared with $$$N$$$ lengthed circular substrings using hash. Hence $$$O(\lceil \frac{N}{K} \rceil \times log(N))$$$
The total time complexity is $$$\approx O(N log(K) + \frac{N}{K} log(N))$$$
For optimal purpose, let $$$K \approx \log{N}$$$ the complexity therefore will be only $$$O(N log(log N))$$$
- Here are the notices:
We are comparing string in circular, you can either use modulo position or duplicated string
Becareful that the real size of string and the duplicated one
- Here are the implementations:
For convention, let just ignore those obvious not a good code (ugly — complex — stupid code worth nothing to say)
V. KMP Approach
1) Normal KMP
In this problem, there is no effeciency using normal KMP over all pair substring comparing, even if we divide into queries and optimize it. It still have no improvement compare to all other implementations
Anyway, here are the implementations
2) Booth Algorithm
Simple Explanation: Remember when you compare circular substrings using hashing ? We find first difference character $$$k$$$ by binary search and compare only at that position to know whether one is greater, equal or less than other. But this give you huge hidden constant that might lead you TLE (the Optimized version doesnt provides that much but its constant is not that small too). While we can take the advantage of $$$KMP$$$ to find its first difference too, in a much smaller constant ofcourse. But we have to do some clever tricks to ignore its high complexity.
Deeper Explanation: For convention let duplicate the initial string $$$S + S$$$. We can easily find the prefix function for the string. We have to find that at position $$$i > n$$$, which position $$$p$$$ that the initial one and the current rotation differ, then compare the two rotations. However it can only provides comparision between inital rotation and the current rotation, if you want for all rotations you have to merge all $$$N$$$ circular substrings. But, since we only care about the minimal lexicographical, we can just immediately eliminate the worse rotation when you find a better rotation. Then we treat initial rotation by this new better a rotation, but by how can we do that ? The important is that we cut the relationship of the current prefix function with the previous one, by assign the previous as $$$-1$$$, hence we are comparing the prefix function for this current rotation. This allow the algorithm complexity drop from normal KMP $$$O(n^2)$$$ to Booth-KMP $$$O(n)$$$ and way much smaller constant than Hashing $$$O(n log n)$$$.
VI. Lyndon Factorization Approach
For detail definitions, proves, applications, implementations, you can read Simple Linear and Effectively Duval Algorithm for Lyndon Factorization
Lyndon word is nonempty string that is strictly smaller in lexicographic order than all of its rotations.
Lyndon factorization is a unique way to split the string into many lyndon words in such a way that the words in the sequence are in non-increasing order. That means we factorize $$$s = s_1 + s_2 + \dots + s_k$$$ where $$$s_1, s_2, \dots, s_k$$$ are lyndon words and in non-increasing order ($$$s1 \geq s2 \geq \dots \geq s_k$$$)
From the above definition, we con conclude that the last factor in lyndon factorization of the string itself is minimal lexicographical.
- Algorithm: In $$$1983$$$, Duval provides an effecient algorithm for listing the Lyndon words of length at most $$$n$$$ with a given alphabet size $$$s$$$ in lexicographic order in $$$O(n)$$$
For string $$$S$$$ of length $$$n$$$
For each new word $$$x$$$ from $$$S$$$ that $$$\forall i$$$ we have $$$x[i] = s[i\ mod\ n]$$$ (mean $$$x$$$ is a sub-string of some cyclic strings that shifted from initial $$$S$$$)
While the last symbol of $$$x$$$ is in sorted order, remove it to make a shorter word
Replace the last remained symbol of $$$x$$$ by its successor in sorted order.
- Example: $$$S = $$$"$$$abaabaaabaababaaabaaababaab$$$"
- Notice: Wait wait wait, are you going to take the head position of last factor of string factorization and it will be the answer ? Nope, becaure the string are circular, you must duplicate the string before doing so, else there might exist such string that start from from right but connect with left to form a minimal lexicographical string rotation.
- Implementation:
VII. Suffix Array Approach
VIII. Suffix Automation Approach
IX. Elimination Tournament Algorithm
So here are my other approachs for this problem. I dont know whether there are names for these algorithm. I just got the ideas from soccer elimination and apply to this problem. By self-researching and computing, finaly I archive linear algorithm
About the algorithm name, if one know any paper about it before, please tag me in and I will tag the first authurs of those/these algorithms
The simplest idea is that we have a list of candidate, and keep doing elimination until we have the final champion
A) Dual Elimination
So the ideas is that for the initial candidate list, we will select 2 consecutive candidates, then compare two circular strings of the length equal to the minimum gap of the two candidates
Let $$$t = s + s$$$, and the selected candidates are $$$l$$$ and $$$r$$$. For convention, let $$$l < r$$$, then we will compare $$$A$$$ and $$$B$$$, for which $$$A = t[l \dots l+(r-l)-1]$$$ and $$$B = t[r \dots r+(r-l)-1]$$$.
Case 1: $$$A < B$$$, we will select the starting point of $$$A$$$ become the next candidate, it is $$$l$$$
Case 2: $$$A > B$$$, we will select the starting point of $$$B$$$ become the next candidate, it is $$$r$$$
Case 3: $$$A = B$$$, we will select the smaller starting point of $$$A$$$ and $$$B$$$, it is $$$min(l, r)$$$
- Example: $$$S = $$$"$$$abaabaaabaababaaabaaababaab$$$"
- Notice: We are comparing circular string !
- Complexity:
Normal Version: Stable Complexity
Optimized Version: Linear when characters are unique
At the $$$k$$$ elimination, the number of participants will be about $$$O(\frac{n}{2^k})$$$
At the $$$k$$$ elimination, each string will be compared $$$O(2^k)$$$ times
Hence the total complexity is $$$O(\overset{\lceil log_n \rceil}{\underset{k = 1}{\Sigma}}(\frac{n}{2^k} \times 2^k))$$$ $$$=$$$ $$$O(n \times 1 + \frac{n}{2} \times 2 + \dots + \frac{n}{2^{\lceil log_2(n) \rceil}} \times \lceil log_2(n) \rceil)$$$ $$$=$$$ $$$O(n \times \lceil log_2(n) \rceil)$$$ $$$=$$$ $$$O(n\ log\ n)$$$
- Implementations:
B) Substring Based Elimination
- Instead of eliminating consecutives candidates, let try to eliminating candidates based on their substring
First, let $$$mn$$$ / $$$mx$$$ is the minimal / maximal character of $$$s$$$
Define: $$$mn = \underset{c \in S}{min}(c)$$$ and $$$mx = \underset{c \in S}{max}(c)$$$
Pre-elimination Round: We take the position $$$p$$$ that $$$s[p] = mn$$$. Since all other positions wont provide Minimal Lexicographical Circular String
Define: $$$candidate = $$$ { $$$p\ \ |\ \ p \in $$$ { $$$0, 1, \dots, |s| - 1$$$ } $$$ \cap s[p] = mn$$$ }
Then we take maximumly $$$n - 1$$$ Rounds from $$$d = 1$$$ to $$$d = n - 1$$$. For all current candidate, add the next character to it. Find the minimal substring, and eliminater unsatisfied others.
Optimization: At the $$$p$$$ round we will find $$$c = $$$ minimal character among all candidate's $$$pth$$$-next character, then traverse again and eliminate the one whose $$$pth$$$-nxt character is greater than $$$c$$$
Define: $$$s[x] = s[x\ mod\ |s|]$$$ and $$$c = \underset{p \in\ candidate}{min}(s[p + d])$$$ and $$$next\ candidate = $$$ { $$$p\ \ |\ \ p \in candidate \cap s[p + d] = c$$$ }
- Example: $$$S = $$$"$$$abaabaaabaababaaabaaababaab$$$"
- Notice: After all eliminations, if there are more than one candidate, select the one with lowest index
- Complexity:
If the string is $$$k$$$-repeated strings (each substring size $$$d$$$) or similar then there will be atleast $$$k$$$ candidate after $$$d$$$ eliminations. About $$$O(k \times d)$$$ in the best cases
Best case to say, like a string where all characters are unique, then it will be Linear $$$O(n)$$$
Worst case to say, like a string where all characters are the same, then it will be $$$O(n \times n) = O(n^2)$$$
- Implementations:
For convention, let not use obviously-not-optimized code
C) Elimination and Colliding
This is the improvement from Substring Based Elimination
It takes me few hours to think and to implement the idea
At the $$$dth$$$ elimination
Let $$$s[p] = s[p\ mod\ |s|]$$$ for convention
Let $$$l, r$$$ are the candidates and $$$l < r$$$
Let $$$A$$$ is a circular substring of $$$s$$$ and $$$A = s[l \dots l + d - 1]$$$
Let $$$B$$$ is a circular substring of $$$s$$$ and $$$B = s[r \dots r + d - 1]$$$
If $$$A$$$ and $$$B$$$ collide ($$$l + d - 1 = r$$$), we will select $$$l$$$ and eliminate $$$r$$$ ($$$l < r$$$)
- If it is still hard to understand, then you can read this proof
- Example: $$$S = $$$"$$$abaabaaabaababaaabaaababaab$$$"
- Notice: Collision Detecting is on Circular Substring
- Complexity:
If the string continues to grown, then they will collide and dies
Trying to extend string size $$$k$$$ mean minimize candidate list size $$$O(k \times \lfloor \frac{n}{k} \rfloor) = O(n)$$$
Trying to maximize candidate list size $$$k$$$ mean reduce the string size $$$O(f(x)) = O(k \times (\lfloor \frac{n}{k} \rfloor \times k - k)) + O(f(\lfloor \frac{k}{2} \rfloor)) = O(k\ log\ k)$$$
- Implementations:
For convention, let not use obviously-not-optimized code
D) Elimination and Merging
This is the improvement from Elimination and Colliding
It takes me few days to think, to prove and to implement the idea
Main Idea: From the above proof, we also have the property that when $$$p$$$ is one of the candidates then $$$repeated-string$$$ start from $$$p$$$ might be one of the candidate
At the $$$dth$$$ elimination, let $$$p_1, p_2, \dots, p_k$$$ are the candidates, $$$A_1, A_2, \dots, A_k$$$ are circular substring of $$$s$$$ that $$$A_1 = s[p_1 \dots p_1 + d - 1]$$$. Notice that all of them are either collide ($$$p_i + d - 1 \equiv p_{i+1}$$$ (mod $$$n$$$)) or not intersect (because the intersect pairs are eliminated at their first collision)
Let $$$v$$$ is the maximum collision or/and exist such consecutive candidates $$$q_1, q_2, \dots, q_v$$$ that $$$A_{q_1}$$$ collides $$$A_{q_2}$$$ collides $$$\dots$$$ collides $$$A_{q_v}$$$. Then $$$A_{q_1}A_{q_2} \dots A_{q_v}$$$ is the best candidates up to the $$$(d \times v)$$$-th elimination.
Therefore, instead of eliminating all the weaker candidates, we can just merge them and teleport to the next elimination that they collides again
- We can prove these above things with contradictions as the same way as the above approach.
- Example: $$$S = $$$"$$$abaabaaabaababaaabaaababaab$$$"
Notice: If the candidates arent changed, teleport to next tournament not itself
Notice: The last candidate might merge with the first candidate
Notice: When merging, you should use modulo operator for safety
Notice: Because of its complex code, you can just ignore pre-elimination and play round $$$d = 1$$$ to $$$n$$$
- Complexity:
It seem to be about $$$O(n\ log\ n)$$$ when each time the candidates is eliminate atleast a half
But actually the complexity is Linear $$$O(n)$$$ because every position is called maximumly once for preprocessing, twice times for comparings, thrice for merging only.
- Implementations: