結果

問題 No.430 文字列検索
ユーザー zundamo-tizundamo-ti
提出日時 2020-11-07 18:44:54
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,127 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 86,700 KB
実行使用メモリ 81,684 KB
最終ジャッジ日時 2023-09-29 20:44:54
合計ジャッジ時間 3,897 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
75,576 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
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()
    N = int(readline())
    
    RH = ROLLINGHASH(S, base, mod)
    ans = 0
    for _ in range(N):
        C = readline().strip()
        l = len(C)
        rh = ROLLINGHASH(C, base, mod).hash[l]
        for i in range(len(S) - l + 1):
            if rh == RH.get_hash(i, i + l):
                ans += 1
    print(ans)

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