結果

問題 No.430 文字列検索
ユーザー rlangevinrlangevin
提出日時 2023-09-07 00:23:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 509 ms / 2,000 ms
コード長 1,075 bytes
コンパイル時間 431 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 137,500 KB
最終ジャッジ日時 2023-09-07 00:24:18
合計ジャッジ時間 6,702 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,400 KB
testcase_01 AC 509 ms
137,500 KB
testcase_02 AC 334 ms
90,344 KB
testcase_03 AC 343 ms
90,544 KB
testcase_04 AC 86 ms
71,444 KB
testcase_05 AC 86 ms
71,624 KB
testcase_06 AC 86 ms
71,704 KB
testcase_07 AC 94 ms
71,588 KB
testcase_08 AC 454 ms
137,004 KB
testcase_09 AC 92 ms
72,336 KB
testcase_10 AC 134 ms
79,752 KB
testcase_11 AC 418 ms
102,248 KB
testcase_12 AC 435 ms
102,264 KB
testcase_13 AC 411 ms
102,384 KB
testcase_14 AC 394 ms
96,528 KB
testcase_15 AC 403 ms
94,332 KB
testcase_16 AC 359 ms
90,500 KB
testcase_17 AC 352 ms
90,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

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)
D = [defaultdict(int) for i in range(11)]
for i in range(1, min(11, len(S) + 1)):
    for j in range(len(S) - i + 1):
        D[i][Sr.get(j, j + i)] += 1
    
ans = 0
M = int(input())
for _ in range(M):
    C = list(input().rstrip())
    if len(C) > len(S):
        continue
    Cr = RollingHash(C)
    ans += D[len(C)][Cr.get(0, len(C))]

print(ans)
0