結果

問題 No.430 文字列検索
ユーザー maizeauxmaizeaux
提出日時 2022-02-08 03:35:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 551 ms / 2,000 ms
コード長 1,432 bytes
コンパイル時間 458 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 105,668 KB
最終ジャッジ日時 2023-09-04 23:35:03
合計ジャッジ時間 8,436 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 170 ms
80,884 KB
testcase_01 AC 540 ms
105,608 KB
testcase_02 AC 537 ms
105,136 KB
testcase_03 AC 526 ms
105,540 KB
testcase_04 AC 167 ms
80,900 KB
testcase_05 AC 167 ms
80,876 KB
testcase_06 AC 168 ms
80,656 KB
testcase_07 AC 168 ms
81,076 KB
testcase_08 AC 205 ms
104,988 KB
testcase_09 AC 172 ms
81,696 KB
testcase_10 AC 177 ms
82,368 KB
testcase_11 AC 551 ms
105,444 KB
testcase_12 AC 539 ms
105,356 KB
testcase_13 AC 538 ms
105,536 KB
testcase_14 AC 542 ms
105,668 KB
testcase_15 AC 548 ms
105,544 KB
testcase_16 AC 536 ms
105,456 KB
testcase_17 AC 531 ms
105,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import math
import sys
import os
from typing import *

sys.setrecursionlimit(100000)
is_local = "TERM_PROGRAM" in os.environ
input = sys.stdin.readline
INF = float('inf')

def debug(*args, **kwargs):
    if is_local:
        print(*args, **kwargs)
def I(): return input().rstrip()
def IS(): return input().split()
def II(): return int(input())
def IIS(): return map(int, input().split())
def LIIS(): return list(map(int, input().split()))

MOD = 2**61 - 1
def main():
    s = I()
    n = len(s)
    hashes = [[] for _ in range(10)]
    hashes[0].append(ord(s[0]) - ord("A"))
    pows = [1]
    for _ in range(10):
        pows.append((pows[-1]*26) % MOD)
    for i in range(10):
        if i >= n:
            break
        if i > 0:
            hashes[i].append((hashes[i-1][0]*26 + ord(s[i]) - ord("A")) % MOD)
        for j in range(n - i - 1):
            x = hashes[i][-1]
            x -= (ord(s[j]) - ord("A")) * pows[i]
            x %= MOD
            x = (x * 26) % MOD
            x += (ord(s[j+i+1]) - ord("A"))
            x %= MOD
            hashes[i].append(x)
    m = II()
    out = 0
    for _ in range(m):
        ss = I()
        l = len(ss)
        h = 0
        for i in range(l):
            h *= 26
            h %= MOD
            h += ord(ss[i]) - ord("A")
            h %= MOD
        out += hashes[l-1].count(h)
    print(out)


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