题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Farmer John is trying to describe his favorite USACO contest to Elsie, but she is having trouble understanding why he likes it so much. He says "My favorite part of the contest was when Bessie said 'It's Mooin' Time' and mooed all over the contest."
Elsie still doesn't understand so Farmer John downloads the contest as a text file and tries to explain what he means. The contest is defined as a string of lowercase letters of length $N$ ($3 \leq N \leq 20\,000$). A moo is generally defined as the substring $c_ic_jc_j$ where some character $c_i$ followed directly by $2$ occurrences of some character $c_j$ where $c_i \neq c_j$. According to Farmer John, Bessie moos a lot, so if some moo appears at least $F$ ($1\le F\le N$) times in the contest, that might be from Bessie.
However, Farmer John's download might have been corrupted, and the text file might have up to one character that differs from the original file. Print all possible moos that Bessie could have made taking the potential error into account, sorted in alphabetical order.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $N$ and $F$, representing the length of the string and the frequency threshold for a moo by Bessie.The second line contains a string of lowercase letters of length $N$, representing the contest.
OUTPUT FORMAT (print output to the terminal / stdout):
Print out the number of possible moos that Bessie makes, followed by a lexicographically sorted list of the moos. Each moo should appear on a separate line.SAMPLE INPUT:
10 2 zzmoozzmoo
SAMPLE OUTPUT:
1 mooIn this case, no character change affects the answer. The only moo Bessie made was "moo".
SAMPLE INPUT:
17 2 momoobaaaaaqqqcqq
SAMPLE OUTPUT:
3 aqq baa cqqIn this case, the $a$ at position $8$ (zero-indexed) could have been corrupted from a $b$ which would have resulted in "baa" being a moo that Bessie made twice. Alternatively, the $q$ at position $11$ could have been corrupted from a $c$ which would have resulted in "cqq" being a possible moo that Bessie made. "aqq" can be made by swapping the $c$ with an $a$.
SAMPLE INPUT:
3 1 ooo
SAMPLE OUTPUT:
25 aoo boo coo doo eoo foo goo hoo ioo joo koo loo moo noo poo qoo roo soo too uoo voo woo xoo yoo zoo
SCORING:
- Inputs 4-8: $N \le 100$
- Inputs 9-13: No additional constraints.
Problem credits: Suhas Nagar
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 11.2k平均输出 token(含思考) ↓
- 33.3输出速度 tokens/s ↑
- 337 秒延迟 ↓
- 0.02×CPU 时间 / 官方解 ↓
- 更优时间复杂度(相对官方解)
- 5Pylint 问题 ↓
- 9.04Pylint 得分 ↑
- 23圈复杂度 ↓
- 57.5可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 10.8k平均输出 token(含思考) ↓
- 187.8输出速度 tokens/s ↑
- 58 秒延迟 ↓
- 0.22×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 8Pylint 问题 ↓
- 8.60Pylint 得分 ↑
- 23圈复杂度 ↓
- 53.1可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
0/1
解出的采样 ↑
- 0.0采样 1 · 运行错误
- 9.9k平均输出 token(含思考) ↓
- 180.3输出速度 tokens/s ↑
- 56 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- —时间复杂度(相对官方解)
- 5Pylint 问题 ↓
- 9.23Pylint 得分 ↑
- 23圈复杂度 ↓
- 51.3可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def solve(): data = sys.stdin.buffer.read().split() if not data: return n = int(data[0]) f = int(data[1]) s = data[2] # Occurrence start positions for each possible moo pattern. occurrences = [[] for _ in range(26 * 26)] for i in range(n - 2): x, y, z = s[i], s[i + 1], s[i + 2] if y == z and x != y: pattern = (x - 97) * 26 + (y - 97) occurrences[pattern].append(i) answer = [] for a in range(26): first = a + 97 for b in range(26): if a == b: continue second = b + 97 starts = occurrences[a * 26 + b] count = len(starts) if count >= f: answer.append(chr(first) + chr(second) * 2) continue # A single character change can increase the frequency of a # particular moo by at most one, since its occurrences cannot overlap. if count + 1 != f: continue covered = bytearray(n) for start in starts: covered[start] = 1 covered[start + 1] = 1 covered[start + 2] = 1 possible = False # Look for a window one mismatch away from this moo. The changed # position must not belong to an existing occurrence, or that # occurrence would be destroyed, giving no net gain. for i in range(n - 2): if s[i] == first: if s[i + 1] == second: if s[i + 2] != second and not covered[i + 2]: possible = True break elif s[i + 2] == second and not covered[i + 1]: possible = True break elif ( s[i + 1] == second and s[i + 2] == second and not covered[i] ): possible = True break if possible: answer.append(chr(first) + chr(second) * 2) output = [str(len(answer))] output.extend(answer) sys.stdout.write("\n".join(output) + "\n") solve()
import sys def main(): data = sys.stdin.buffer.read().split() if not data: return N = int(data[0]) F = int(data[1]) s = data[2] arr = [ch - 97 for ch in s] # bytes values to 0..25 window_count = N - 2 old_pattern = [-1] * window_count cnt = [0] * (26 * 26) # Count original moo occurrences. for j in range(window_count): a = arr[j] b = arr[j + 1] c = arr[j + 2] if b == c and a != b: code = a * 26 + b old_pattern[j] = code cnt[code] += 1 possible = [False] * (26 * 26) for code in range(26 * 26): if cnt[code] >= F: possible[code] = True # Try changing each position to each possible letter. for i in range(N): lo = i - 2 if lo < 0: lo = 0 hi = i if hi > window_count - 1: hi = window_count - 1 for new_char in range(26): delta = {} # Only windows containing position i can change. for j in range(lo, hi + 1): old = old_pattern[j] if old != -1: delta[old] = delta.get(old, 0) - 1 a = new_char if j == i else arr[j] b = new_char if j + 1 == i else arr[j + 1] c = new_char if j + 2 == i else arr[j + 2] if b == c and a != b: code = a * 26 + b delta[code] = delta.get(code, 0) + 1 for code, change in delta.items(): if cnt[code] + change >= F: possible[code] = True answers = [] for code in range(26 * 26): if possible[code]: x = code // 26 y = code % 26 answers.append(chr(97 + x) + chr(97 + y) * 2) out = [str(len(answers))] out.extend(answers) sys.stdout.write("\n".join(out)) if __name__ == "__main__": main()
import sys def solve(): data = sys.stdin.buffer.read().split() if not data: return n = int(data[0]) f = int(data[1]) s = data[2] # bytes: indexing returns character codes def moo_id(a, b, c): """Return an ID for a valid moo, or -1 otherwise.""" if b == c and a != b: return a * 26 + b return -1 # Original frequency of each possible moo. freq = [0] * (26 * 26) old_ids = [-1] * (n - 2) for i in range(n - 2): mid = moo_id(s[i], s[i + 1], s[i + 2]) old_ids[i] = mid if mid != -1: freq[mid] += 1 possible = [False] * (26 * 26) # The downloaded file might already be correct. for mid, count in enumerate(freq): if count >= f: possible[mid] = True alphabet = range(26) for p in range(n): # Starts of length-3 substrings containing position p. starts = [] if p >= 2: starts.append(p - 2) if p >= 1 and p - 1 <= n - 3: starts.append(p - 1) if p <= n - 3: starts.append(p) original = s[p] for replacement in alphabet: if replacement == original: continue # Change in frequency caused by this replacement. delta = {} for start in starts: old_id = old_ids[start] if start == p: new_id = moo_id(replacement, s[start + 1], s[start + 2]) elif start == p - 1: new_id = moo_id(s[start], replacement, s[start + 2]) else: new_id = moo_id(s[start], s[start + 1], replacement) if old_id == new_id: continue if old_id != -1: delta[old_id] = delta.get(old_id, 0) - 1 if new_id != -1: delta[new_id] = delta.get(new_id, 0) + 1 for mid, change in delta.items(): if freq[mid] + change >= f: possible[mid] = True answers = [] for mid, valid in enumerate(possible): if valid: first = mid // 26 second = mid % 26 answers.append( chr(ord('a') + first) + chr(ord('a') + second) + chr(ord('a') + second) ) answers.sort() output = [str(len(answers))] output.extend(answers) sys.stdout.write("\n".join(output) + "\n") if __name__ == "__main__": solve()