結果

問題 No.464 PPAP
ユーザー rpy3cpprpy3cpp
提出日時 2017-04-14 05:05:09
言語 PyPy3
(7.3.13)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,838 bytes
コンパイル時間 388 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 469,552 KB
最終ジャッジ日時 2023-09-25 17:01:21
合計ジャッジ時間 14,952 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,300 KB
testcase_01 AC 74 ms
71,296 KB
testcase_02 AC 73 ms
71,356 KB
testcase_03 AC 78 ms
71,352 KB
testcase_04 AC 83 ms
76,408 KB
testcase_05 AC 79 ms
75,568 KB
testcase_06 AC 77 ms
75,464 KB
testcase_07 AC 140 ms
92,692 KB
testcase_08 AC 122 ms
84,732 KB
testcase_09 AC 109 ms
90,496 KB
testcase_10 TLE -
testcase_11 AC 809 ms
328,216 KB
testcase_12 AC 1,515 ms
468,812 KB
testcase_13 AC 1,256 ms
469,100 KB
testcase_14 AC 1,627 ms
468,672 KB
testcase_15 AC 76 ms
71,484 KB
testcase_16 AC 74 ms
71,220 KB
testcase_17 AC 74 ms
71,316 KB
testcase_18 AC 73 ms
71,508 KB
testcase_19 AC 77 ms
75,596 KB
testcase_20 AC 80 ms
75,504 KB
testcase_21 AC 79 ms
75,296 KB
testcase_22 AC 78 ms
75,564 KB
testcase_23 AC 1,231 ms
469,228 KB
testcase_24 AC 1,244 ms
469,552 KB
testcase_25 AC 1,249 ms
469,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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] を合計すればよい。
    '''
    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] * (N + 1) for _ in range(N)]
    for a in range(N):
        for b in range(a, N + 1):
            cumP[a][b] = cumP[a][b - 1] + isP[a][b]
    return cumP

def count_PPAP(isP, cumP):
    N = len(isP)
    count = 0
    for a in range(N):
        if isP[0][a]:
            for b in range(a + 1, N):
                if isP[b][N]:
                    count += cumP[a][b - 1]
    return count

S = input().rstrip()
print(solve(S))
0