結果
| 問題 |
No.464 PPAP
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-04-14 05:23:19 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,060 bytes |
| コンパイル時間 | 189 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 398,080 KB |
| 最終ジャッジ日時 | 2024-07-18 13:25:22 |
| 合計ジャッジ時間 | 10,877 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 20 TLE * 2 |
ソースコード
def solve(S):
'''S=PPAP となる{PPAP}の組が何通りあるかを返す。
Pは、1文字以上の回文。Aは1文字以上の任意の文字列。
isP[a][b] : S[a:b] が回文なら True、そうでないなら False
isP[a][b] = (a + 1 == b) or (isP[a+1][b-1] and (S[a] == S[b-1]))
cumP[a][b] := sum(isP[a][k] for k in range(a + 1, b))
= cumP[a][b-1] + isP[a][b]
S[0:a], S[a:c], S[c:b], S[b:N] と分割したとき、
isP[0][a] = True
isP[b][N] = True
isP[a][c] = True
a < c < b となる(a, c, b) の組み合わせを求めたい。
(a, b) を固定したとき、条件を満たす c の個数は、cumP[a][b - 1] なので、
isP[0][a] == isP[b][N] == True を満たす (a, b) について cumP[a][b - 1] を合計すればよい。
'''
if len(set(S)) == 1:
N = len(S)
return (N - 1) * (N - 2) * (N - 3) // 6
isP = init_isP(S)
cumP = init_cumP(S, isP)
return count_PPAP(isP, cumP)
def init_isP(S):
N = len(S)
isP = [[False] * (N + 1) for _ in range(N)]
for c in range(N):
isP[c][c + 1] = True
for w in range(1, min(c + 1, N - c)):
if S[c - w] != S[c + w]:
break
isP[c - w][c + w + 1] = True
for w in range(1, min(c + 1, N + 1 - c)):
if S[c - w] != S[c + w - 1]:
break
isP[c - w][c + w] = True
return isP
def init_cumP(S, isP):
N = len(S)
cumP = [[0] for _ in range(N)]
for a in range(N):
if not isP[0][a]:
continue
cumPa = [0] * (N + 1)
isPa = isP[a]
for b in range(a, N + 1):
cumPa[b] = cumPa[b - 1] + isPa[b]
cumP[a] = cumPa
return cumP
def count_PPAP(isP, cumP):
N = len(isP)
count = 0
for a in range(N):
if isP[0][a]:
cumPa = cumP[a]
for b in range(a + 1, N):
if isP[b][N]:
count += cumPa[b - 1]
return count
S = input().rstrip()
print(solve(S))