結果
問題 |
No.430 文字列検索
|
ユーザー |
![]() |
提出日時 | 2022-09-27 11:45:13 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 205 ms / 2,000 ms |
コード長 | 1,376 bytes |
コンパイル時間 | 164 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 133,120 KB |
最終ジャッジ日時 | 2024-11-10 01:01:55 |
合計ジャッジ時間 | 2,594 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 |
ソースコード
from collections import defaultdict class RollingHash(): def __init__(self, str, MOD, B): self.hash = [0]*(len(str)+1) self.B_power = [1]*(max(11,len(str)+1)) for i in range(10): self.B_power[i+1] = self.B_power[i]*B % MOD 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)