結果

問題 No.599 回文かい
ユーザー rlangevinrlangevin
提出日時 2023-10-18 12:19:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,537 ms / 4,000 ms
コード長 1,198 bytes
コンパイル時間 1,317 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 91,564 KB
最終ジャッジ日時 2023-10-18 12:19:14
合計ジャッジ時間 7,591 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
55,500 KB
testcase_01 AC 46 ms
55,500 KB
testcase_02 AC 45 ms
55,500 KB
testcase_03 AC 45 ms
55,500 KB
testcase_04 AC 55 ms
63,868 KB
testcase_05 AC 97 ms
72,800 KB
testcase_06 AC 47 ms
55,556 KB
testcase_07 AC 48 ms
55,556 KB
testcase_08 AC 47 ms
55,556 KB
testcase_09 AC 63 ms
68,140 KB
testcase_10 AC 68 ms
70,436 KB
testcase_11 AC 68 ms
70,460 KB
testcase_12 AC 72 ms
73,096 KB
testcase_13 AC 84 ms
76,608 KB
testcase_14 AC 774 ms
89,744 KB
testcase_15 AC 118 ms
77,152 KB
testcase_16 AC 1,353 ms
90,760 KB
testcase_17 AC 1,537 ms
91,564 KB
testcase_18 AC 49 ms
61,396 KB
testcase_19 AC 50 ms
61,396 KB
testcase_20 AC 51 ms
61,400 KB
evil_0.txt AC 1,203 ms
82,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
from functools import lru_cache
mod = 10 ** 9 + 7

def main():
    @lru_cache(maxsize=None)
    def f(start):
        val = 1
        ind = 1
        goal = len(S) - start
        while start + ind <= goal - ind:
            if R.get(start, start + ind) == R.get(goal - ind, goal):
                val += f(start + ind)
                val %= mod
            ind += 1  
        return val

    class RollingHash():
        def __init__(self, s):
            self._mod = 2 ** 32 - 1
            self._base = 4649
            
            n = len(s)
            self.h = [0] * (n + 1)
            self.pw = [1] * (n + 1)
    
            for i in range(n):
                self.h[i + 1] = self.h[i] * self._base + ord(s[i])
                self.h[i + 1] %= self._mod
    
            for i in range(n):
                self.pw[i + 1] = self.pw[i] * self._base
                self.pw[i + 1] %= self._mod
                
            """
            s[l:r]のhash値
            """
        def get(self, l, r):
            return (self.h[r] - self.h[l] * self.pw[r - l]) % self._mod
        
    S = list(input())
    R = RollingHash(S)
    print(f(0))

main()
0