結果

問題 No.430 文字列検索
ユーザー tktk_snsntktk_snsn
提出日時 2020-08-13 16:13:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 626 ms / 2,000 ms
コード長 1,543 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 53,628 KB
最終ジャッジ日時 2024-04-18 01:37:08
合計ジャッジ時間 7,083 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
10,880 KB
testcase_01 AC 626 ms
53,628 KB
testcase_02 AC 524 ms
16,768 KB
testcase_03 AC 537 ms
16,896 KB
testcase_04 AC 27 ms
10,752 KB
testcase_05 AC 26 ms
10,880 KB
testcase_06 AC 26 ms
10,752 KB
testcase_07 AC 25 ms
10,880 KB
testcase_08 AC 607 ms
53,200 KB
testcase_09 AC 26 ms
10,624 KB
testcase_10 AC 75 ms
14,208 KB
testcase_11 AC 554 ms
21,324 KB
testcase_12 AC 549 ms
21,456 KB
testcase_13 AC 550 ms
21,588 KB
testcase_14 AC 527 ms
19,228 KB
testcase_15 AC 527 ms
19,072 KB
testcase_16 AC 533 ms
16,768 KB
testcase_17 AC 523 ms
16,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


class RollingHash:
    def __init__(self, S, base=1007, mod=(1 << 61) - 1):
        """
        input
            S : ハッシュ化したい文字列、配列の場合はatoiメソッドをいじること
            base : 基数
            mod : hashを丸めるやつ。基本そのまま使う
        """
        self.size = len(S)
        self.base = base
        self.mod = mod
        self.h_table = [0] * (self.size + 1)
        self.power = [0] * (self.size + 1)
        self.power[0] = 1
        for i, s in enumerate(S):
            s = self.atoi(s)
            self.h_table[i + 1] = (self.h_table[i] * base + s) % mod
            self.power[i + 1] = self.power[i] * base % mod

    @staticmethod
    def atoi(a):
        return ord(a) - ord("a") + 1

    def get(self, L, R):
        """閉区間S[L, R)のhash値を返す()"""
        res = self.h_table[R] - self.h_table[L] * self.power[R - L] % self.mod
        return res % self.mod

    def calc_hash(self, S):
        res = 0
        for s in S:
            res = (res * self.base + self.atoi(s)) % self.mod
        return res


S = input().strip()
N = len(S)

RH = RollingHash(S)
memo = [defaultdict(int) for _ in range(11)]
for L in range(1, 11):
    for i in range(N - L + 1):
        h = RH.get(i, i + L)
        memo[L][h] += 1

ans = 0
M = int(input())
for _ in range(M):
    c = input().strip()
    h = RH.calc_hash(c)
    ans += memo[len(c)][h]

print(ans)
0