結果
| 問題 |
No.273 回文分解
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-26 15:43:57 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 57 ms / 2,000 ms |
| コード長 | 1,657 bytes |
| コンパイル時間 | 182 ms |
| コンパイル使用メモリ | 82,052 KB |
| 実行使用メモリ | 61,440 KB |
| 最終ジャッジ日時 | 2025-03-26 15:44:09 |
| 合計ジャッジ時間 | 3,039 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
s = input().strip()
n = len(s)
# Precompute palindrome array
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
else:
if s[i] == s[j]:
if j == i + 1:
pal[i][j] = True
else:
pal[i][j] = pal[i+1][j-1]
# Precompute split_valid array
split_valid = [[False] * n for _ in range(n)]
for i in range(n-1, -1, -1):
for j in range(i, n):
if i == j:
split_valid[i][j] = True
else:
if pal[i][j]:
split_valid[i][j] = True
else:
for k in range(i, j):
if pal[i][k] and split_valid[k+1][j]:
split_valid[i][j] = True
break
# Collect all possible palindromic substrings and sort by length descending
candidates = []
for i in range(n):
for j in range(i, n):
if pal[i][j]:
candidates.append((j - i + 1, i, j))
candidates.sort(reverse=True, key=lambda x: x[0])
max_len = 0
found = False
for length, i, j in candidates:
if length == n:
continue # Skip the whole string as it's not allowed
left_possible = False
right_possible = False
if i > 0:
left_possible = split_valid[0][i-1]
if j < n-1:
right_possible = split_valid[j+1][n-1]
if left_possible or right_possible:
max_len = length
found = True
break
if found:
print(max_len)
else:
# If no split possible other than individual characters, result is 1
print(1)
lam6er