結果

問題 No.430 文字列検索
ユーザー zundamo-tizundamo-ti
提出日時 2020-11-07 18:51:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 269 ms / 2,000 ms
コード長 1,248 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 86,900 KB
実行使用メモリ 143,280 KB
最終ジャッジ日時 2023-09-29 20:45:20
合計ジャッジ時間 4,036 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,320 KB
testcase_01 AC 269 ms
143,280 KB
testcase_02 AC 161 ms
79,716 KB
testcase_03 AC 159 ms
79,848 KB
testcase_04 AC 94 ms
71,560 KB
testcase_05 AC 94 ms
71,544 KB
testcase_06 AC 93 ms
71,500 KB
testcase_07 AC 95 ms
71,792 KB
testcase_08 AC 214 ms
137,208 KB
testcase_09 AC 105 ms
76,588 KB
testcase_10 AC 119 ms
82,276 KB
testcase_11 AC 186 ms
88,348 KB
testcase_12 AC 187 ms
88,240 KB
testcase_13 AC 185 ms
88,144 KB
testcase_14 AC 175 ms
83,436 KB
testcase_15 AC 176 ms
81,576 KB
testcase_16 AC 166 ms
79,536 KB
testcase_17 AC 159 ms
79,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

class ROLLINGHASH:
    def __init__(self, string, base, mod):
        self.size = len(string)
        self.base = base
        self.mod = mod
        self.hash = [0]*(self.size + 1)
        self.pow = [1]*(self.size + 1)
        
        for i, c in enumerate(string):
            o = ord(c) - ord('A') + 1
            self.hash[i + 1] = (self.hash[i] * self.base + o) % self.mod
            self.pow[i + 1] = self.pow[i] * self.base % self.mod

    def get_hash(self, l, r):
        """s[l:r]のhash値"""
        ret = (self.hash[r] - self.hash[l] * self.pow[r - l]) % self.mod
        return ret

def main():
    base = 37
    mod = 10**9 + 7
    S = readline().strip()
    M = int(readline())
    L = len(S)
    
    RH = ROLLINGHASH(S, base, mod)
    d = defaultdict(int)
    for length in range(1, 11):
        for l in range(L - length + 1):
            r = l + length
            h = RH.get_hash(l, r)
            d[h] += 1
    
    ans = 0
    for _ in range(M):
        C = input()
        h = ROLLINGHASH(C, base, mod).hash[len(C)]
        ans += d[h]
    print(ans)

if __name__ == "__main__":
   main()
0