結果

問題 No.430 文字列検索
ユーザー RainkunchRainkunch
提出日時 2022-09-27 11:41:43
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,284 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 145,172 KB
最終ジャッジ日時 2023-08-24 03:43:59
合計ジャッジ時間 4,117 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,612 KB
testcase_01 AC 243 ms
145,172 KB
testcase_02 AC 145 ms
81,560 KB
testcase_03 AC 148 ms
81,540 KB
testcase_04 AC 88 ms
71,360 KB
testcase_05 AC 88 ms
71,664 KB
testcase_06 AC 88 ms
71,552 KB
testcase_07 AC 89 ms
71,664 KB
testcase_08 AC 195 ms
138,780 KB
testcase_09 RE -
testcase_10 AC 111 ms
82,636 KB
testcase_11 AC 170 ms
90,660 KB
testcase_12 AC 173 ms
90,664 KB
testcase_13 AC 170 ms
90,708 KB
testcase_14 AC 165 ms
85,772 KB
testcase_15 AC 158 ms
83,660 KB
testcase_16 AC 148 ms
81,400 KB
testcase_17 AC 151 ms
81,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
class RollingHash():
    def __init__(self, str, MOD, B):
        self.hash = [0]*(len(str)+1)
        self.B_power = [1]*(len(str)+1)
        for i in range(len(str)): # 累積和的に求めていく
            self.hash[i+1] = (self.hash[i]*B + ord(str[i])) % MOD
            self.B_power[i+1] = self.B_power[i]*B % MOD

    def Bad_rollinghash(self,s): # ローリングハッシュの定義
        H = 0 
        for i,si in enumerate(s):
            H += ord(si)*self.B_power[len(s)-i-1]
            H %= MOD
        return H # これでは毎回O(m)かかってしまい、意味がない

    def get_hash(self,l,r): # str[l,r)のハッシュ値を求める
        return (self.hash[r] - self.hash[l]*self.B_power[r-l]) % MOD

    def debug(self):
        print(self.hash)
        print(self.B_power)

S = list(input())
M = int(input())
MOD = 10**9+9
B = 37
d = defaultdict(int)

Ryh = RollingHash(S,MOD,B)
# Cは高々10文字なので、Sの1~10文字分のハッシュ値をdictに記録していく
for width in range(1,11):
    for i in range(len(S)-width+1):
        h = Ryh.get_hash(i,i+width)
        d[h] += 1

ans = 0
for i in range(M):
    C = list(input())
    C_hash = Ryh.Bad_rollinghash(C)
    ans += d[C_hash]
print(ans)
0