結果
| 問題 | No.273 回文分解 |
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-31 17:20:33 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 59 ms / 2,000 ms |
| コード長 | 1,402 bytes |
| 記録 | |
| コンパイル時間 | 227 ms |
| コンパイル使用メモリ | 96,108 KB |
| 実行使用メモリ | 83,328 KB |
| 最終ジャッジ日時 | 2026-07-08 03:56:49 |
| 合計ジャッジ時間 | 3,964 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
s = input().strip()
n = len(s)
if n < 2:
print(1)
exit()
# Precompute palindrome table
pal = [[False] * n for _ in range(n)]
for i in range(n - 1, -1, -1):
for j in range(i, n):
if i == j:
pal[i][j] = True
elif j == i + 1:
pal[i][j] = (s[i] == s[j])
else:
pal[i][j] = (s[i] == s[j]) and pal[i + 1][j - 1]
# Initialize dp arrays
dp0 = [-1] * (n + 1) # dp0[i] represents the case where the first i characters form a palindrome without splitting
dp1 = [-1] * (n + 1) # dp1[i] represents the case where at least one split is made
dp0[0] = 0 # Base case: zero characters
# Fill dp0: check if the substring from 0 to i-1 is a palindrome
for i in range(1, n + 1):
if pal[0][i - 1]:
dp0[i] = i
else:
dp0[i] = -1
# Fill dp1
for i in range(1, n + 1):
max_val = -1
for j in range(1, i): # j ranges from 1 to i-1 to split into two non-empty parts
if pal[j][i - 1]:
current_max = -1
if dp0[j] != -1:
current_max = max(dp0[j], i - j)
if dp1[j] != -1:
candidate = max(dp1[j], i - j)
if candidate > current_max:
current_max = candidate
if current_max != -1 and current_max > max_val:
max_val = current_max
dp1[i] = max_val
print(dp1[n] if dp1[n] != -1 else 0)
lam6er