結果

問題 No.599 回文かい
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-14 00:07:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,808 ms / 4,000 ms
コード長 1,530 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 82,116 KB
実行使用メモリ 76,328 KB
最終ジャッジ日時 2024-05-02 08:23:57
合計ジャッジ時間 12,770 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,896 KB
testcase_01 AC 37 ms
52,824 KB
testcase_02 AC 37 ms
53,148 KB
testcase_03 AC 37 ms
52,300 KB
testcase_04 AC 60 ms
67,504 KB
testcase_05 AC 55 ms
65,768 KB
testcase_06 AC 60 ms
69,136 KB
testcase_07 AC 62 ms
68,400 KB
testcase_08 AC 61 ms
68,028 KB
testcase_09 AC 60 ms
68,180 KB
testcase_10 AC 905 ms
75,868 KB
testcase_11 AC 602 ms
75,784 KB
testcase_12 AC 1,070 ms
76,212 KB
testcase_13 AC 630 ms
75,936 KB
testcase_14 AC 1,522 ms
76,328 KB
testcase_15 AC 1,741 ms
76,272 KB
testcase_16 AC 1,614 ms
75,880 KB
testcase_17 AC 1,808 ms
76,060 KB
testcase_18 AC 42 ms
59,836 KB
testcase_19 AC 42 ms
59,496 KB
testcase_20 AC 40 ms
60,912 KB
evil_0.txt AC 1,186 ms
76,324 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