結果

問題 No.430 文字列検索
ユーザー brthyyjpbrthyyjp
提出日時 2021-03-12 14:36:32
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,133 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 84,736 KB
最終ジャッジ日時 2024-04-22 02:09:51
合計ジャッジ時間 3,706 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
57,472 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 #

class RollingHash():
    def __init__(self, s, base, mod):
        self.mod = mod
        n = len(s)

        self.hash = [0]*(n+1)
        self.power = [1]*(n+1)

        v = 0
        for i in range(n):
            self.hash[i+1] = (v*base+s[i])%mod
            v = (v*base+s[i])%mod
        v = 1
        for i in range(n):
            self.power[i+1] = v*base%mod
            v = v*base%mod

    def get(self, l, r):
        # [l, r)のhashを取得
        return (self.hash[r]-self.hash[l]*self.power[r-l])%self.mod

base = 1<<30
mod1 = (1<<61)-1
mod2 = (1<<31)-1

import sys
import io, os
input = sys.stdin.readline

S = str(input().rstrip())
n = len(S)
m = int(input())
S = [ord(c)-ord('A') for c in S]
rhs1 = RollingHash(S, base, mod1)
rhs2 = RollingHash(S, base, mod2)
ans = 0
for i in range(m):
    C = str(input().rstrip())
    C = [ord(c)-ord('A') for c in C]
    rhc1 = RollingHash(C, base, mod1)
    rhc2 = RollingHash(C, base, mod2)
    temp = 0
    for l in range(n-len(C)+1):
        if rhs1.get(l, l+len(C)) == rhc1.get(0, len(C)) and rhs2.get(l, l+len(C)) == rhc2.get(0, len(C)) :
            ans += 1
print(ans)
0