結果

問題 No.205 マージして辞書順最小
ユーザー lam6er
提出日時 2025-03-31 17:52:42
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 951 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 82,116 KB
実行使用メモリ 856,980 KB
最終ジャッジ日時 2025-03-31 17:53:34
合計ジャッジ時間 7,245 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other AC * 2 MLE * 1 -- * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from functools import lru_cache

def main():
    sys.setrecursionlimit(1 << 25)
    n = int(sys.stdin.readline())
    strings = [sys.stdin.readline().strip() for _ in range(n)]

    @lru_cache(maxsize=None)
    def dfs(state):
        remaining = [s for s in state if s]
        if not remaining:
            return ''
        first_chars = [s[0] for s in remaining]
        min_char = min(first_chars)
        candidates_indices = [i for i, s in enumerate(state) if s and s[0] == min_char]
        min_str = None
        for idx in candidates_indices:
            new_state = list(state)
            new_state[idx] = new_state[idx][1:]
            new_state_tuple = tuple(new_state)
            res = min_char + dfs(new_state_tuple)
            if min_str is None or res < min_str:
                min_str = res
        return min_str

    initial_state = tuple(strings)
    print(dfs(initial_state))

if __name__ == '__main__':
    main()
0