結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
Rainkunch
|
| 提出日時 | 2022-09-27 11:41:43 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,284 bytes |
| コンパイル時間 | 176 ms |
| コンパイル使用メモリ | 82,048 KB |
| 実行使用メモリ | 132,992 KB |
| 最終ジャッジ日時 | 2024-11-10 01:01:53 |
| 合計ジャッジ時間 | 2,458 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 13 RE * 1 |
ソースコード
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)
Rainkunch