結果

問題 No.430 文字列検索
ユーザー FuyuruFuyuru
提出日時 2023-10-24 22:12:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 277 ms / 2,000 ms
コード長 1,204 bytes
コンパイル時間 1,261 ms
コンパイル使用メモリ 81,484 KB
実行使用メモリ 112,844 KB
最終ジャッジ日時 2023-10-24 22:12:25
合計ジャッジ時間 4,060 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,452 KB
testcase_01 AC 277 ms
112,844 KB
testcase_02 AC 161 ms
77,420 KB
testcase_03 AC 161 ms
77,404 KB
testcase_04 AC 41 ms
55,480 KB
testcase_05 AC 41 ms
55,480 KB
testcase_06 AC 42 ms
55,480 KB
testcase_07 AC 43 ms
55,480 KB
testcase_08 AC 249 ms
111,992 KB
testcase_09 AC 48 ms
61,372 KB
testcase_10 AC 81 ms
80,476 KB
testcase_11 AC 194 ms
86,364 KB
testcase_12 AC 193 ms
86,364 KB
testcase_13 AC 188 ms
86,364 KB
testcase_14 AC 189 ms
81,876 KB
testcase_15 AC 174 ms
79,892 KB
testcase_16 AC 167 ms
77,524 KB
testcase_17 AC 165 ms
77,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


class RollingHash:
    def __init__(self,N,mod=(1<<61)-1,base=37):
        self.N = N
        self.mod = mod
        self.base = base
        self.pw = [1]*(self.N+1)
        tmp = 1
        for i in range(self.N+1):
            self.pw[i] = tmp
            tmp = tmp*self.base%self.mod
        self.hs = [0]*(self.N+1)
    def build_from_int(self,S):
        tmp = 0
        for i in range(self.N):
            tmp = (tmp*self.base+S[i]+1)%self.mod
            self.hs[i+1] = tmp
    def build_from_str(self,S):
        tmp = 0
        for i in range(self.N):
            tmp = (tmp*self.base+ord(S[i]))%self.mod
            self.hs[i+1] = tmp
    def get_hash(self,l,r):
        return (self.hs[r]-self.hs[l]*self.pw[r-l])%self.mod
    def make_hash(self,S):
        tmp = 0
        for i in range(len(S)):
            tmp = (tmp*self.base+ord(S[i]))&self.mod
        return tmp


S = input()
N = len(S)
M = int(input())
rh = RollingHash(N)
rh.build_from_str(S)
d = defaultdict(lambda:0)
for i in range(1,min(N,10)+1):
    for j in range(N-i+1):
        d[rh.get_hash(j,j+i)] += 1
ans = 0
for i in range(M):
    C = input()
    ans += d[rh.make_hash(C)]
print(ans)
0