結果

問題 No.430 文字列検索
ユーザー eSeFeSeF
提出日時 2022-05-16 18:55:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,218 ms / 2,000 ms
コード長 1,617 bytes
コンパイル時間 129 ms
コンパイル使用メモリ 11,008 KB
実行使用メモリ 97,284 KB
最終ジャッジ日時 2023-10-12 12:46:12
合計ジャッジ時間 13,070 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
9,008 KB
testcase_01 AC 1,218 ms
97,284 KB
testcase_02 AC 956 ms
16,984 KB
testcase_03 AC 980 ms
17,092 KB
testcase_04 AC 19 ms
8,916 KB
testcase_05 AC 19 ms
8,968 KB
testcase_06 AC 20 ms
8,864 KB
testcase_07 AC 21 ms
8,920 KB
testcase_08 AC 1,136 ms
97,172 KB
testcase_09 AC 24 ms
8,868 KB
testcase_10 AC 111 ms
15,668 KB
testcase_11 AC 1,026 ms
28,368 KB
testcase_12 AC 1,045 ms
28,216 KB
testcase_13 AC 1,074 ms
28,264 KB
testcase_14 AC 1,019 ms
23,152 KB
testcase_15 AC 1,008 ms
20,500 KB
testcase_16 AC 993 ms
17,260 KB
testcase_17 AC 937 ms
16,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random
class Rolling_Hash:
    def __init__(self, S):
        self.S = S
        self.MOD = (1<<61)-1
        #self.B = random.randint(1<<12, 1<<28)
        self.B = 94627281
        self.N = len(S)

        self.hs = [0] * (self.N+1)
        self.pw = [1] * (self.N+1)
        self.pwinv = [1] * (self.N+1)
        for i in range(self.N):
            self.hs[i+1] = (ord(S[i]) * self.pw[i] + self.hs[i]) % self.MOD
            self.pw[i+1] = (self.B * self.pw[i]) % self.MOD
        self.pwinv[self.N] = pow(self.pw[self.N], -1, self.MOD)
        for i in range(self.N):
            j = self.N - i - 1
            self.pwinv[j] = (self.pwinv[j+1] * self.B) % self.MOD

    def hashvalue(self, idx):
        return self.hs[idx]

    def hash_substr(self, l, r): # [l, r)
        subhash = (self.hs[r] - self.hs[l])
        return (subhash * self.pwinv[l]) % self.MOD
    
    def hash_segment(self, startidx, length):
        return self.hash_substr(startidx, startidx + length)
    
    def get_hasharray(self):
        return self.hs

S = input()
RH = Rolling_Hash(S)
dict = {}

ds = {}
N = len(S)
for L in range(1, 11):
    for i in range(N-L+1):
        hs = RH.hash_segment(i, L)
        
        if hs in dict:
            dict[hs] += 1
        else:
            dict[hs] = 1

        if S[i:i+L] in ds:
            ds[S[i:i+L]] += 1
        else:
            ds[S[i:i+L]] = 1


#print(dict)

M = int(input())
ans = 0
for i in range(M):
    C = input()
    RHC = Rolling_Hash(C)
    hsc = RHC.hash_substr(0,len(C))
    if hsc in dict:
        #print('exist', C, hsc)
        ans += dict[hsc]
print(ans)
0