結果

問題 No.430 文字列検索
ユーザー irumo8202irumo8202
提出日時 2022-01-24 16:53:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 824 bytes
コンパイル時間 406 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 21,916 KB
最終ジャッジ日時 2024-05-08 10:58:53
合計ジャッジ時間 4,377 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
17,696 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

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