結果

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

ソースコード

diff #

n = int(input())
from collections import defaultdict

group = defaultdict(int)

for _ in range(n):
    s = input().strip()
    if not s:
        continue
    # Check if the string is good
    good = True
    prev = s[0]
    for c in s[1:]:
        if c < prev:
            good = False
            break
        prev = c
    if not good:
        continue
    a = s[0]
    b = s[-1]
    length = len(s)
    key = (a, b)
    if group[key] < length:
        group[key] = length

# Convert grouped items into tuples (a, b, length) with a and b as integers
tuples = []
for (a_char, b_char), length in group.items():
    a = ord(a_char) - ord('a')
    b = ord(b_char) - ord('a')
    tuples.append((a, b, length))

# Sort the tuples by a ascending, then by length descending, then by b ascending
tuples.sort(key=lambda x: (x[0], -x[2], x[1]))

dp = [0] * 26
prefix_max = [0] * 26

for a, b, length in tuples:
    current_max_prev = prefix_max[a]
    new_value = current_max_prev + length
    if new_value > dp[b]:
        dp[b] = new_value
    # Update the prefix_max array
    new_prefix = [0] * 26
    new_prefix[0] = dp[0]
    for i in range(1, 26):
        new_prefix[i] = max(new_prefix[i-1], dp[i])
    prefix_max = new_prefix

print(max(dp))
0