結果

問題 No.599 回文かい
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-14 00:07:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,952 ms / 4,000 ms
コード長 1,530 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 86,840 KB
実行使用メモリ 77,680 KB
最終ジャッジ日時 2023-08-14 20:21:13
合計ジャッジ時間 14,072 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
71,048 KB
testcase_01 AC 66 ms
71,024 KB
testcase_02 AC 63 ms
71,172 KB
testcase_03 AC 66 ms
71,016 KB
testcase_04 AC 111 ms
76,084 KB
testcase_05 AC 84 ms
76,396 KB
testcase_06 AC 86 ms
75,996 KB
testcase_07 AC 88 ms
76,408 KB
testcase_08 AC 83 ms
76,236 KB
testcase_09 AC 81 ms
76,096 KB
testcase_10 AC 961 ms
77,224 KB
testcase_11 AC 601 ms
77,156 KB
testcase_12 AC 1,068 ms
77,448 KB
testcase_13 AC 662 ms
77,276 KB
testcase_14 AC 1,641 ms
77,532 KB
testcase_15 AC 1,807 ms
77,680 KB
testcase_16 AC 1,740 ms
77,316 KB
testcase_17 AC 1,952 ms
77,592 KB
testcase_18 AC 68 ms
75,312 KB
testcase_19 AC 72 ms
75,408 KB
testcase_20 AC 71 ms
75,292 KB
evil_0.txt AC 1,308 ms
77,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class RollingHash:
    def __init__(self, S, base=1007, mod=(1 << 61) - 1):
        """
        input
            S : ハッシュ化したい文字列、配列の場合はatoiメソッドをいじること
            base : 基数
            mod : hashを丸めるやつ。基本そのまま使う
        """
        self.size = len(S)
        self.base = base
        self.mod = mod
        self.h_table = [0] * (self.size + 1)
        self.power = [0] * (self.size + 1)
        self.power[0] = 1
        for i, s in enumerate(S):
            s = self.atoi(s)
            self.h_table[i + 1] = (self.h_table[i] * base + s) % mod
            self.power[i + 1] = self.power[i] * base % mod

    @staticmethod
    def atoi(a):
        return ord(a) - ord("a") + 1

    def get(self, L, R):
        """閉区間S[L, R)のhash値を返す()"""
        res = self.h_table[R] - self.h_table[L] * self.power[R - L] % self.mod
        return res % self.mod

    def calc_hash(self, S):
        res = 0
        for s in S:
            res = (res * self.base + self.atoi(s)) % self.mod
        return res


mod = 10 ** 9 + 7
S = input()
RH = RollingHash(S)


def get_opposite(l, r):
    return len(S) - r, len(S) - l


def is_match(l, r):
    ll, rr = get_opposite(l, r)
    return RH.get(l, r) == RH.get(ll, rr)


N = len(S) // 2
dp = [0] * (N + 1)
dp[0] = 1

ans = 0
for r in range(N + 1):
    for l in range(r):
        if is_match(l, r):
            dp[r] += dp[l]
            dp[r] %= mod
    ans += dp[r]
    ans %= mod
print(ans)
0