結果
| 問題 | 
                            No.430 文字列検索
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-11-07 18:44:54 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,127 bytes | 
| コンパイル時間 | 181 ms | 
| コンパイル使用メモリ | 82,432 KB | 
| 実行使用メモリ | 79,872 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:48:42 | 
| 合計ジャッジ時間 | 3,608 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | -- * 4 | 
| other | AC * 1 TLE * 1 -- * 12 | 
ソースコード
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()