結果

問題 No.430 文字列検索
ユーザー irumo8202irumo8202
提出日時 2022-01-24 16:53:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,626 ms / 2,000 ms
コード長 824 bytes
コンパイル時間 561 ms
コンパイル使用メモリ 87,048 KB
実行使用メモリ 79,016 KB
最終ジャッジ日時 2023-08-21 05:27:38
合計ジャッジ時間 17,790 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
70,860 KB
testcase_01 AC 1,625 ms
78,312 KB
testcase_02 AC 1,534 ms
78,208 KB
testcase_03 AC 1,521 ms
78,320 KB
testcase_04 AC 73 ms
71,104 KB
testcase_05 AC 74 ms
71,288 KB
testcase_06 AC 71 ms
71,056 KB
testcase_07 AC 77 ms
71,176 KB
testcase_08 AC 95 ms
76,924 KB
testcase_09 AC 81 ms
75,600 KB
testcase_10 AC 90 ms
76,104 KB
testcase_11 AC 1,616 ms
78,328 KB
testcase_12 AC 1,524 ms
78,184 KB
testcase_13 AC 1,521 ms
78,244 KB
testcase_14 AC 1,616 ms
78,256 KB
testcase_15 AC 1,525 ms
78,212 KB
testcase_16 AC 1,626 ms
78,484 KB
testcase_17 AC 1,520 ms
79,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 1-dimension Rolling Hash
class RollingHash:
    def __init__(self, s, base, mod):
        self.mod = mod
        self.pw = pw = [1] * (len(s) + 1)

        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


# 非クラス版
base = 37
mod = 10 ** 9 + 9


S = input()
N = len(S)

RHS = RollingHash(S, base, mod)

M = int(input())

ans = 0

for i in range(M):
    C = input()
    K = len(C)
    RH = RollingHash(C, base, mod)
    hash = RH.get(0, K)

    for i in range(N - K + 1):
        if RHS.get(i, i + K) == hash:
            ans += 1

print(ans)
0