Minimum representation
1. Principle of minimum representation
Problem: given a string, we can rotate the string arbitrarily to return the string representation with the smallest dictionary order.
-
Assuming that the given string is s, we first copy s to the tail of S. assuming that the string length is n, when we enumerate the starting point start and return 0~n-1, we can get the representation of all strings.
-
Initially let i=0, j=1, compare s[i+k] and s[j+k], and use K to represent the offset position relative to I and J. if it is equal, let K + + until the first unequal position is compared. Assuming s[i+k] > s[j+k], it indicates that the string starting from i~i+k is not the minimum representation of the original string, and I can be updated to i+k+1; Conversely, you can update J.
-
In the update process, if i==j, you need to stagger the two. You can let i + + and finally return min(i, j). This position is the starting point of the minimum representation corresponding to the string.
2. Minimum representation problem on acwing
AcWing 158. Necklace
Problem description
-
Question link: AcWing 158. Necklace
analysis
- Find the minimum representation of two strings and compare whether they are equal.
code
- C++
#include <iostream> #include <cstring> using namespace std; const int N = 2000010; int n; char a[N], b[N]; int get_min(char s[]) { int i = 0, j = 1; while (i < n && j < n) { int k = 0; while (k < n && s[i + k] == s[j + k]) k++; if (s[i + k] > s[j + k]) i += k + 1; else j += k + 1; if (i == j) i++; } int k = min(i, j); s[k + n] = 0; return k; } int main() { scanf("%s%s", a, b); n = strlen(a); memcpy(a + n, a, n); memcpy(b + n, b, n); int x = get_min(a), y = get_min(b); if (strcmp(a + x, b + y)) puts("No"); else { puts("Yes"); puts(a + x); } return 0; }
3. Minimum representation on force buckle
Leetcode 0796 rotate string
Title Description: Leetcode 0796 rotate string
analysis
-
The test point of this question: minimum representation.
-
Find the minimum representation of two strings and compare whether they are equal.
-
Assuming that the given string is s, we first copy s to the tail of S. assuming that the string length is n, when we enumerate the starting point start and return 0~n-1, we can get the representation of all strings.
-
Initially let i=0, j=1, compare s[i+k] and s[j+k], and use K to represent the offset position relative to I and J. if it is equal, let K + + until the first unequal position is compared. Assuming s[i+k] > s[j+k], it indicates that the string starting from i~i+k is not the minimum representation of the original string, and I can be updated to i+k+1; Conversely, you can update J.
-
In the update process, if i==j, you need to stagger the two. You can let i + + and finally return min(i, j). This position is the starting point of the minimum representation corresponding to the string.
code
- C++
class Solution { public: bool rotateString(string A, string B) { if (A.size() != B.size()) return false; return get_min(A) == get_min(B); } string get_min(string s) { int n = s.size(); s = s + s; int i = 0, j = 1; while (i < n && j < n) { int k = 0; while (k < n && s[i + k] == s[j + k]) k++; if (s[i + k] > s[j + k]) i += k + 1; else j += k + 1; if (i == j) i++; } int k = min(i, j); return s.substr(k, n); } };
- Java
class Solution { public boolean rotateString(String A, String B) { if (A.length() != B.length()) return false; return get_min(A).equals(get_min(B)); } private String get_min(String s) { int n = s.length(); StringBuilder sb = new StringBuilder(); sb.append(s).append(s); char[] cs = sb.toString().toCharArray(); int i = 0, j = 1; while (i < n && j < n) { int k = 0; while (k < n && cs[i + k] == cs[j + k]) k++; if (cs[i + k] > cs[j + k]) i += k + 1; else j += k + 1; if (i == j) i++; } int k = Math.min(i, j); return sb.substring(k, k + n).toString(); } }
Spatiotemporal complexity analysis
-
Time complexity: O ( n ) O(n) O(n), n is the string length.
-
Space complexity: O ( n ) O(n) O(n).