結果

問題 No.599 回文かい
ユーザー rlangevinrlangevin
提出日時 2023-10-18 12:15:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,319 ms / 4,000 ms
コード長 1,194 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 82,788 KB
実行使用メモリ 94,104 KB
最終ジャッジ日時 2024-09-18 08:37:27
合計ジャッジ時間 9,979 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

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, goal):
        val = 1
        ind = 1
        while start + ind <= goal - ind:
            if R.get(start, start + ind) == R.get(goal - ind, goal):
                val += f(start + ind, goal - 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, len(S)))

main()
0