結果

問題 No.430 文字列検索
ユーザー marroncastlemarroncastle
提出日時 2022-02-06 16:35:14
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 865 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 86,860 KB
実行使用メモリ 83,532 KB
最終ジャッジ日時 2023-09-02 07:00:35
合計ジャッジ時間 4,056 ms
ジャッジサーバーID
(参考情報)
judge16 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,464 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
# 参考 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 = 1<<61-1
S = input()
N = len(S)
M = int(input())
RHS = RollingHash(S,base,mod)
ans = 0
for i in range(M):
    C = input()
    HC = RollingHash(C,base,mod)
    ans += len([1 for i in range(N-len(C)+1)if RHS.get(i,i+len(C))==HC.all()])
print(ans)
0