結果

問題 No.1512 作文
ユーザー lam6er
提出日時 2025-03-20 20:26:15
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,530 bytes
コンパイル時間 478 ms
コンパイル使用メモリ 82,820 KB
実行使用メモリ 121,888 KB
最終ジャッジ日時 2025-03-20 20:27:37
合計ジャッジ時間 4,870 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21 WA * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin

def main():
    n = int(stdin.readline())
    valid_strings = []
    for _ in range(n):
        s = stdin.readline().strip()
        good = True
        for i in range(1, len(s)):
            if s[i] < s[i-1]:
                good = False
                break
        if good:
            start = s[0]
            end = s[-1]
            length = len(s)
            valid_strings.append((start, end, length))
    
    dp = [0] * 26
    max_dp = [0] * 26
    dp_prev = 0  # Represents the initial state (no characters chosen)
    
    # Initialize max_dp based on initial dp values
    current_max = 0
    for i in range(26):
        current_max = max(current_max, dp[i])
        max_dp[i] = current_max
    
    for start, end, length in valid_strings:
        a = ord(start) - ord('a')
        b = ord(end) - ord('a')
        # Calculate the maximum possible value before appending this string
        max_before = max(dp_prev, max_dp[a])
        candidate = max_before + length
        # Update dp[b] if candidate is larger
        if candidate > dp[b]:
            dp[b] = candidate
        # Recalculate the max_dp array after updating dp
        current_max = 0
        new_max_dp = []
        for i in range(26):
            current_max = max(current_max, dp[i])
            new_max_dp.append(current_max)
        max_dp = new_max_dp
    
    # The answer is the maximum value between the initial state and the best in max_dp
    print(max(max_dp[-1], dp_prev))

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