結果

問題 No.430 文字列検索
ユーザー chomeyamachomeyama
提出日時 2019-08-10 02:11:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,767 bytes
コンパイル時間 771 ms
コンパイル使用メモリ 10,868 KB
実行使用メモリ 27,656 KB
最終ジャッジ日時 2023-09-26 23:04:59
合計ジャッジ時間 3,955 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
14,292 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

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, h):
    B, val = pow(b, x-1, h), 0
    t = B
    v = 0
    for i, c in enumerate(s):
        # print(i, c, t)
        v = (v + CHRtoINT[c] * t) % h
        t //= b
    t = B
    for i, c in enumerate(S[:x]):
        # print(i, c, t)
        val = (val + CHRtoINT[c] * t) % h
        t //= b
    if v == val:
        ret = 1
    else:
        ret = 0
    hash = [val] + [None]*(len(S)-x)
    B *= b
    for i, c in enumerate(S[:-x]):
        # print(i, c, B)
        val = (val * b - CHRtoINT[c] * B + CHRtoINT[S[i+x]]) % h
        if val == v:
            ret += 1
        hash[i+1] = val
    # print(hash, v)
    # return hash
    return ret

S = input()
M = int(input())
ans = 0
for i in range(M):
    s = input()
    ans += RollingHash(S, s, len(s), 5, 10**9+7)
    # print(ans)
print(ans)


# N, D = map(int, input().split())
# X, Y = map(int, input().split())
# if X % D or Y % D:
#     print(0)
#     exit()
# X //= D
# Y //= D
# mod = 10**9+7
# def factrial(N):
#     fc = [1, 1] + [None] * N
#     inv = [0, 1] + [None] * N
#     fcInv = [1, 1] + [None] * N
#     for i in range(2, N+1):
#         fc[i] = i*fc[i-1]%mod
#         inv[i] = mod-(inv[mod%i]*(mod//i))%mod
#         fcInv[i] = fcInv[i-1]*inv[i]%mod
#     return fc, fcInv
# fc, fcInv = factrial(N+1)
# def modCom(n, k):
#     return 0 if n < k else fc[n] * fcInv[k] * fcInv[n-k] % mod
# for k in range(0, N+1):
#     for i in range(k+1):
#         px = X+
0