結果

問題 No.1512 作文
ユーザー gew1fw
提出日時 2025-06-12 14:13:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,026 bytes
コンパイル時間 140 ms
コンパイル使用メモリ 82,452 KB
実行使用メモリ 114,128 KB
最終ジャッジ日時 2025-06-12 14:13:52
合計ジャッジ時間 4,057 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21 WA * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    
    good_strings = []
    for _ in range(N):
        s = input[idx]
        idx += 1
        valid = True
        for i in range(1, len(s)):
            if s[i] < s[i-1]:
                valid = False
                break
        if valid:
            start = s[0]
            end = s[-1]
            s_idx = ord(start) - ord('a')
            e_idx = ord(end) - ord('a')
            good_strings.append((s_idx, e_idx, len(s)))
    
    dp = [0] * 26
    max_dp = [0] * 26
    
    for s_idx, e_idx, l in good_strings:
        current_max = max_dp[s_idx]
        candidate = current_max + l
        if candidate > dp[e_idx]:
            dp[e_idx] = candidate
        # Update max_dp
        current_max = 0
        for i in range(26):
            if dp[i] > current_max:
                current_max = dp[i]
            max_dp[i] = current_max
    
    print(max_dp[-1])

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