Replies: 14 comments 2 replies
-
最小表示法其实可以用后缀数组做的,复杂度也可以到O(n) |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
使用SA-IS做到O(n) |
Beta Was this translation helpful? Give feedback.
-
@hfhongzy 谢谢,涨姿势了。 |
Beta Was this translation helpful? Give feedback.
-
博主你好,问一下,为什么答案是(i,j)的较小值,i不行吗 |
Beta Was this translation helpful? Give feedback.
-
因为你是随机选一个跳啊,有可能你跳了之后$i>j$了就会出问题 |
Beta Was this translation helpful? Give feedback.
-
后缀数组有$O(n)$做法的 |
Beta Was this translation helpful? Give feedback.
-
不不不,想了下,还是需要求min(i, j) Java代码如下所示: public static int[] getMin(int[] arr) {
int i = 0;
int j = 1;
int k = 0;
int n = arr.length;
while (i < n && j < n && k < n) {
if (arr[(i + k) % n] < arr[(j + k) % n]) {
j = j + k + 1;
k = 0;
} else if (arr[(i + k) % n] > arr[(j + k) % n]) {
i = i + k + 1;
k = 0;
} else {
k++;
}
if (i == j) {
j++;
}
}
i = Math.min(i, j);
int[] res = new int[arr.length];
for(k = 0; k < arr.length; k++) {
res[k] = arr[(i + k) % n];
}
return res;
} |
Beta Was this translation helpful? Give feedback.
-
最后return 的不应该是 |
Beta Was this translation helpful? Give feedback.
-
假设arr.length为n |
Beta Was this translation helpful? Give feedback.
-
跳的时候可以一直保证 i < j来跳嘛? 先让 i = j; j++ |
Beta Was this translation helpful? Give feedback.
-
始终保证 i < j 的写法 def get_min(s):
n = len(s)
i, j = 0, 1
while j < n:
k = 0
while k < n and s[(i + k) % n] == s[(j + k) % n]:
k += 1
if s[(i + k) % n] > s[(j + k) % n]:
i, j = j, max(j + 1, i + k + 1)
else:
j = j + k + 1
return s[i:] + s[:i] |
Beta Was this translation helpful? Give feedback.
-
https://oi-wiki.org/string/minimal-string/
Beta Was this translation helpful? Give feedback.
All reactions