結果

問題 No.430 文字列検索
ユーザー chomeyamachomeyama
提出日時 2019-08-10 02:55:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 1,095 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 11,044 KB
実行使用メモリ 11,188 KB
最終ジャッジ日時 2023-09-26 23:07:02
合計ジャッジ時間 2,177 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
9,972 KB
testcase_01 AC 267 ms
10,484 KB
testcase_02 AC 60 ms
11,056 KB
testcase_03 AC 70 ms
11,188 KB
testcase_04 AC 31 ms
9,932 KB
testcase_05 AC 30 ms
9,936 KB
testcase_06 AC 31 ms
9,980 KB
testcase_07 AC 32 ms
9,936 KB
testcase_08 AC 54 ms
10,084 KB
testcase_09 AC 32 ms
9,944 KB
testcase_10 AC 37 ms
9,944 KB
testcase_11 AC 96 ms
10,660 KB
testcase_12 AC 70 ms
11,084 KB
testcase_13 AC 71 ms
11,072 KB
testcase_14 AC 68 ms
11,140 KB
testcase_15 AC 69 ms
11,164 KB
testcase_16 AC 63 ms
11,116 KB
testcase_17 AC 63 ms
11,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from fractions import Fraction

CHRtoINT = defaultdict(int)
AtoZ = [chr(i) for i in range(65,65+26)]
for i, c in enumerate(AtoZ):
    CHRtoINT[c] = i

def RollingHash(S, s, x, b=26, m=67280421310721):
    B, val, se = pow(b, x-1, m), 0, set()
    for _s in s:
        v, t = 0, B
        for c in _s:
            v = (v + CHRtoINT[c] * t) % m
            t //= b
        se.add(v)
    t = B
    for c in S[:x]:
        val = (val + CHRtoINT[c] * t) % m
        t //= b
    ret = int(val in se)
    B *= b
    for i, c in enumerate(S[:-x]):
        val = (val * b - CHRtoINT[c] * B + CHRtoINT[S[i+x]]) % m
        ret += int(val in se)
    return ret

S = input()
M = int(input())
s = [input() for i in range(M)]
s.sort(key=lambda x: len(x))
l, r, ans = 0, 0, 0
for k in range(len(s[0]), len(s[-1])+1):
    while r < M and len(s[r]) == k:
        r += 1
    if l != r:
        ans += RollingHash(S, s[l:r], k)
        l = r
print(ans)
0