結果

問題 No.430 文字列検索
ユーザー 👑 timitimi
提出日時 2022-12-04 21:48:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,445 ms / 2,000 ms
コード長 1,177 bytes
コンパイル時間 127 ms
コンパイル使用メモリ 82,316 KB
実行使用メモリ 77,484 KB
最終ジャッジ日時 2024-04-20 04:04:41
合計ジャッジ時間 11,484 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
58,832 KB
testcase_01 AC 979 ms
77,484 KB
testcase_02 AC 1,445 ms
75,536 KB
testcase_03 AC 966 ms
76,892 KB
testcase_04 AC 37 ms
59,416 KB
testcase_05 AC 35 ms
58,308 KB
testcase_06 AC 35 ms
57,844 KB
testcase_07 AC 35 ms
58,168 KB
testcase_08 AC 47 ms
64,244 KB
testcase_09 AC 40 ms
60,652 KB
testcase_10 AC 47 ms
64,232 KB
testcase_11 AC 973 ms
77,180 KB
testcase_12 AC 970 ms
77,172 KB
testcase_13 AC 963 ms
76,996 KB
testcase_14 AC 975 ms
77,192 KB
testcase_15 AC 972 ms
77,028 KB
testcase_16 AC 983 ms
76,968 KB
testcase_17 AC 982 ms
77,352 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
pw = None
def rolling_hash(s):
    l = len(s)
    h = [0]*(l + 1)
    v = 0
    for i in range(l):
        h[i+1] = v = (v * base + ord(s[i])) % mod
    return h
# RH前に、必要な長さの最大値分のpow-tableを計算しておく
def setup_pw(l):
    global pw
    pw = [1]*(l + 1)
    v = 1
    for i in range(l):
        pw[i+1] = v = v * base % mod
def get(h, l, r):
    return (h[r] - h[l] * pw[r-l]) % mod
  
setup_pw(5*10**4)
S=input()
M=int(input())
A=rolling_hash(S)
ans=0
for i in range(M):
  T=input()
  B=rolling_hash(T)[-1]
  for j in range(len(S)-len(T)+1):
    d=get(A,j,j+len(T))
    if d==B:
      ans+=1
    #print(d,j,j+len(T))
print(ans)
0