結果

問題 No.430 文字列検索
ユーザー marroncastlemarroncastle
提出日時 2022-02-06 16:53:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 910 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 87,376 KB
実行使用メモリ 118,648 KB
最終ジャッジ日時 2023-09-02 07:01:12
合計ジャッジ時間 4,788 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,412 KB
testcase_01 AC 339 ms
118,648 KB
testcase_02 AC 222 ms
80,328 KB
testcase_03 AC 222 ms
80,320 KB
testcase_04 AC 90 ms
71,756 KB
testcase_05 AC 89 ms
71,756 KB
testcase_06 AC 88 ms
71,912 KB
testcase_07 AC 90 ms
71,736 KB
testcase_08 AC 284 ms
116,628 KB
testcase_09 AC 101 ms
76,828 KB
testcase_10 AC 124 ms
81,648 KB
testcase_11 AC 251 ms
86,556 KB
testcase_12 AC 251 ms
86,368 KB
testcase_13 AC 254 ms
86,604 KB
testcase_14 AC 249 ms
83,328 KB
testcase_15 AC 237 ms
82,772 KB
testcase_16 AC 226 ms
80,228 KB
testcase_17 AC 224 ms
80,292 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 = 10**9+7; mod = 1<<61-1
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