翻译为Java语言 def construct_string(t, k): m = len(t) s = '' suffixes = [t[i:] for i in range(m)] counts = [0] * m for i in range(k * m): suffix = suffixes[i % m] s += suffix[0] counts[m-len(suffix)] += 1 if counts[m-len(suffix)] == k: # 如果该后缀出现次数已经达到 k ,则停止添加该后缀 suffixes.remove(suffix) m -= len(suffix) counts.pop(m) if not suffixes: # 如果所有后缀都已经添加完毕,则结束循环 break return s
时间: 2023-12-03 14:03:33 浏览: 75
PHP_NETWORK_GETADDRESSES: GETADDRINFO FAILED问题解决办法
5星 · 资源好评率100%
public static String constructString(String t, int k) {
int m = t.length();
String s = "";
List<String> suffixes = new ArrayList<>();
for (int i = 0; i < m; i++) {
suffixes.add(t.substring(i));
}
int[] counts = new int[m];
for (int i = 0; i < k * m; i++) {
String suffix = suffixes.get(i % m);
s += suffix.charAt(0);
counts[m - suffix.length()] += 1;
if (counts[m - suffix.length()] == k) {
suffixes.remove(suffix);
m -= suffix.length();
counts = Arrays.copyOfRange(counts, 0, m);
if (suffixes.isEmpty()) {
break;
}
}
}
return s;
}
阅读全文