Minimal Representation
Minimal string representation is a method used to solve problems concerning what its name shows (i.e. minimal string representation itself).
Minimum representation of the string¶
Cyclic isomorphism¶
When a position
Then we can say that
Minimal representation¶
The minimal representation of the string
The simple brute force¶
Each time we compare the isomorphism of the loop starting from
int k = 0, i = 0, j = 1;
while (k < n && i < n && j < n) {
if (sec[(i + k) % n] == sec[(j + k) % n]) {
++k;
} else {
if (sec[(i + k) % n] > sec[(j + k) % n])
++i;
else
++j;
k = 0;
if (i == j) i++;
}
}
i = min(i, j);
The performance is good using random data, but it may get stuck with special cases.
For example: for string
We find that when there are multiple consecutive repeated substrings in the string, the efficiency of this algorithm is reduced. So we have to consider optimizing this process.
Minimal representation algorithm¶
The principle of the algorithm¶
For a pair of strings
First we consider the case where
So when comparing, we can skip the subscript
In this way, we have completed the optimization of the above brute force solution.
Time complexity¶
Steps of the algorithm¶
- Initialize pointer
i 0 j 1 k 0 - Compare the size of the
k - Repeat the process above until the comparisons are over;
- The answer is the smaller one of
i,j
Code¶
int k = 0, i = 0, j = 1;
while (k < n && i < n && j < n) {
if (sec[(i + k) % n] == sec[(j + k) % n]) {
k++;
} else {
sec[(i + k) % n] > sec[(j + k) % n] ? i = i + k + 1 : j = j + k + 1;
if (i == j) i++;
k = 0;
}
}
i = min(i, j);
buildLast update and/or translate time of this article,Check the history
editFound smelly bugs? Translation outdated? Wanna contribute with us? Edit this Page on Github
peopleContributor of this article OI-wiki
translateTranslator of this article Visit the original article!
copyrightThe article is available under CC BY-SA 4.0 & SATA ; additional terms may apply.