結果

問題 No.430 文字列検索
ユーザー rlangevinrlangevin
提出日時 2023-09-07 00:15:24
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 967 bytes
コンパイル時間 486 ms
コンパイル使用メモリ 87,296 KB
実行使用メモリ 93,796 KB
最終ジャッジ日時 2023-09-07 00:15:28
合計ジャッジ時間 4,327 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
76,136 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
input = sys.stdin.readline

class RollingHash():
    def __init__(self, s):
        self._mod = 2 ** 64 - 1
        self._base = 4649
        
        n = len(s)
        self.h = [0] * (n + 1)
        self.pw = [1] * (n + 1)

        for i in range(n):
            self.h[i + 1] = self.h[i] * self._base + ord(s[i])
            self.h[i + 1] %= self._mod

        for i in range(n):
            self.pw[i + 1] = self.pw[i] * self._base
            self.pw[i + 1] %= self._mod
            
        """
        s[l:r]のhash値
        """
    def get(self, l, r):
        return (self.h[r] - self.h[l] * self.pw[r - l]) % self._mod
    
    
S = list(input().rstrip())
Sr = RollingHash(S)
ans = 0
M = int(input())
for _ in range(M):
    C = list(input().rstrip())
    if len(C) > len(S):
        continue
    Cr = RollingHash(C)
    for i in range(len(S) - len(C) + 1):
        if Cr.get(0, len(C)) == Sr.get(i, i + len(C)):
            ans += 1

print(ans)
0