結果
問題 | No.1512 作文 |
ユーザー |
![]() |
提出日時 | 2025-06-12 19:14:41 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,241 bytes |
コンパイル時間 | 169 ms |
コンパイル使用メモリ | 82,704 KB |
実行使用メモリ | 78,148 KB |
最終ジャッジ日時 | 2025-06-12 19:15:08 |
合計ジャッジ時間 | 4,897 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 21 WA * 17 |
ソースコード
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))