結果

問題 No.205 マージして辞書順最小
ユーザー lam6er
提出日時 2025-04-16 15:56:04
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,454 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 81,992 KB
実行使用メモリ 84,492 KB
最終ジャッジ日時 2025-04-16 15:58:23
合計ジャッジ時間 8,342 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other AC * 3 TLE * 1 -- * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

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))
0