結果

問題 No.430 文字列検索
ユーザー brthyyjpbrthyyjp
提出日時 2021-03-12 15:10:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 261 ms / 2,000 ms
コード長 1,289 bytes
コンパイル時間 420 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 157,184 KB
最終ジャッジ日時 2024-04-22 02:42:58
合計ジャッジ時間 3,969 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,016 KB
testcase_01 AC 261 ms
156,672 KB
testcase_02 AC 170 ms
79,456 KB
testcase_03 AC 167 ms
79,360 KB
testcase_04 AC 40 ms
53,760 KB
testcase_05 AC 40 ms
53,888 KB
testcase_06 AC 40 ms
54,144 KB
testcase_07 AC 40 ms
54,272 KB
testcase_08 AC 222 ms
157,184 KB
testcase_09 AC 54 ms
63,488 KB
testcase_10 AC 85 ms
77,952 KB
testcase_11 AC 196 ms
90,496 KB
testcase_12 AC 202 ms
90,752 KB
testcase_13 AC 194 ms
91,008 KB
testcase_14 AC 195 ms
83,968 KB
testcase_15 AC 189 ms
81,920 KB
testcase_16 AC 176 ms
79,232 KB
testcase_17 AC 169 ms
79,360 KB
権限があれば一括ダウンロードができます

ソースコード

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

base1 = 1007
mod1 = 10**9+7

base2 = 2009
mod2 = 10**9+9

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

S = str(input().rstrip())
n = len(S)
m = int(input())
S = [ord(c)-ord('A')+1 for c in S]
rhs1 = RollingHash(S, base1, mod1)
rhs2 = RollingHash(S, base2, mod2)
from collections import defaultdict
M = [defaultdict(lambda: 0) for i in range(11)]
for i in range(1, 11):
    for l in range(n-i+1):
        h1 = rhs1.get(l, l+i)
        h2 = rhs2.get(l, l+i)
        M[i][(h1, h2)] += 1

ans = 0
for i in range(m):
    C = str(input().rstrip())
    C = [ord(c)-ord('A')+1 for c in C]
    rhc1 = RollingHash(C, base1, mod1)
    rhc2 = RollingHash(C, base2, mod2)
    h1 = rhc1.get(0, len(C))
    h2 = rhc2.get(0, len(C))
    ans += M[len(C)][(h1, h2)]
print(ans)
0