結果

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

ソースコード

diff #

import sys

def main():
    N = int(sys.stdin.readline())
    good_strings = []
    for _ in range(N):
        s = sys.stdin.readline().strip()
        is_good = True
        for i in range(1, len(s)):
            if s[i] < s[i-1]:
                is_good = False
                break
        if is_good:
            s_char = s[0]
            e_char = s[-1]
            length = len(s)
            good_strings.append((s_char, e_char, length))
    
    # 按照结束字符排序
    good_strings.sort(key=lambda x: x[1])
    
    dp = [0] * 26
    prefix_max = [0] * 26
    
    for s_char, e_char, length in good_strings:
        s_idx = ord(s_char) - ord('a')
        e_idx = ord(e_char) - ord('a')
        
        current_max = prefix_max[s_idx] + length
        if current_max > dp[e_idx]:
            dp[e_idx] = current_max
        
        # 更新prefix_max
        prefix_max[0] = dp[0]
        for i in range(1, 26):
            prefix_max[i] = max(prefix_max[i-1], dp[i])
    
    print(max(dp))

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