結果

問題 No.205 マージして辞書順最小
ユーザー lam6er
提出日時 2025-04-16 15:58:33
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,123 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 849,372 KB
最終ジャッジ日時 2025-04-16 16:00:40
合計ジャッジ時間 6,937 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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):
        candidates = []
        min_char = '{'  # A character higher than 'z' to ensure initial comparison
        for i in range(n):
            s = state[i]
            if s:
                c = s[0]
                if c < min_char:
                    min_char = c
                    candidates = [i]
                elif c == min_char:
                    candidates.append(i)
        if not candidates:
            return ''
        min_result = None
        for i in candidates:
            new_state = list(state)
            new_state[i] = new_state[i][1:]
            new_state_tuple = tuple(new_state)
            res = min_char + dfs(new_state_tuple)
            if (min_result is None) or (res < min_result):
                min_result = res
        return min_result

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

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