結果

問題 No.430 文字列検索
ユーザー marroncastlemarroncastle
提出日時 2022-02-06 16:54:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 905 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 87,324 KB
実行使用メモリ 143,604 KB
最終ジャッジ日時 2023-09-02 07:01:17
合計ジャッジ時間 4,033 ms
ジャッジサーバーID
(参考情報)
judge15 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,776 KB
testcase_01 AC 266 ms
143,604 KB
testcase_02 AC 161 ms
79,836 KB
testcase_03 AC 159 ms
79,916 KB
testcase_04 AC 92 ms
71,644 KB
testcase_05 AC 92 ms
71,356 KB
testcase_06 AC 93 ms
71,400 KB
testcase_07 AC 93 ms
71,804 KB
testcase_08 AC 200 ms
137,288 KB
testcase_09 AC 101 ms
76,720 KB
testcase_10 AC 116 ms
82,328 KB
testcase_11 AC 183 ms
88,736 KB
testcase_12 AC 186 ms
88,844 KB
testcase_13 AC 186 ms
88,736 KB
testcase_14 AC 176 ms
84,088 KB
testcase_15 AC 172 ms
81,708 KB
testcase_16 AC 160 ms
79,556 KB
testcase_17 AC 158 ms
79,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 1-dimension Rolling Hash
# 参考 https://tjkendev.github.io/procon-library/python/string/rolling_hash.html

class RollingHash():
    def __init__(self, s, base, mod):
        self.mod = mod
        self.pw = pw = [1]*(len(s)+1)
        self.length = l = len(s)
        self.h = h = [0]*(l+1)

        v = 0
        for i in range(l): h[i+1] = v = (v * base + ord(s[i])) % mod
        v = 1
        for i in range(l): pw[i+1] = v = v * base % mod

    def get(self, l, r):
        return (self.h[r] - self.h[l] * self.pw[r-l]) % self.mod
    
    def all(self):
        return self.get(0, self.length)

base = 37; mod = 10**9+7
S = input()
N = len(S)
M = int(input())
RHS = RollingHash(S,base,mod)
from collections import defaultdict
dic = defaultdict(lambda: 0)
for l in range(1,11):
    for i in range(N-l+1):dic[RHS.get(i,i+l)]+=1
print(sum(dic[RollingHash(input(),base,mod).all()]for _ in range(M)))
0