結果
| 問題 |
No.205 マージして辞書順最小
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-04-15 22:24:27 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,454 bytes |
| コンパイル時間 | 168 ms |
| コンパイル使用メモリ | 81,836 KB |
| 実行使用メモリ | 84,692 KB |
| 最終ジャッジ日時 | 2025-04-15 22:26:54 |
| 合計ジャッジ時間 | 7,682 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | AC * 3 TLE * 1 -- * 11 |
ソースコード
import functools
n = int(input())
strings = [input().strip() for _ in range(n)]
current_strings = [s for s in strings if s]
result = []
while current_strings:
# Collect the first characters of all non-empty strings
heads = [s[0] for s in current_strings]
min_char = min(heads)
# Find all indices where the first character is min_char
candidates = [i for i, s in enumerate(current_strings) if s[0] == min_char]
best_combined = None
best_new_list = None
for idx in candidates:
# Create a copy of the current strings and modify the chosen one
new_list = [s[:] for s in current_strings]
new_list[idx] = new_list[idx][1:]
# Remove any empty strings
filtered = [s for s in new_list if s]
# Sort the remaining strings using the custom comparator
sorted_list = sorted(filtered, key=functools.cmp_to_key(lambda a, b: -1 if (a + b) < (b + a) else 1))
# Generate the combined string for comparison
combined = min_char + ''.join(sorted_list)
# Check if this is the best candidate so far
if best_combined is None or combined < best_combined:
best_combined = combined
best_new_list = filtered
# Append the chosen character to the result
result.append(min_char)
# Update current_strings to the best_new_list for the next iteration
current_strings = best_new_list
print(''.join(result))
lam6er