結果

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

ソースコード

diff #

import bisect

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    
    n = int(data[0])
    strings = []
    idx = 1
    for _ in range(n):
        s = data[idx]
        idx += 1
        is_good = True
        for i in range(1, len(s)):
            if s[i] < s[i-1]:
                is_good = False
                break
        if is_good:
            head = s[0]
            tail = s[-1]
            strings.append((head, tail, len(s)))
    
    if not strings:
        print(0)
        return
    
    strings.sort(key=lambda x: x[1])
    tails = [x[1] for x in strings]
    
    dp = []
    max_dp = 0
    overall_max = 0
    
    for i in range(len(strings)):
        head, tail, length = strings[i]
        pos = bisect.bisect_right(tails, head, 0, i)
        k = pos - 1
        current_max = 0
        if k >= 0:
            if k < len(dp):
                current_max = dp[k]
            else:
                current_max = 0
        current_dp = length
        if k >= 0:
            current_dp = max(current_dp, current_max + length)
        dp.append(current_dp)
        if i == 0:
            max_dp = current_dp
        else:
            max_dp = max(max_dp, current_dp)
        overall_max = max(overall_max, max_dp)
    
    print(overall_max)

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