結果

問題 No.430 文字列検索
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-01-05 07:23:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,070 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 26,524 KB
最終ジャッジ日時 2024-04-23 10:55:18
合計ジャッジ時間 3,694 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
16,256 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
S = list(input())
M = int(input())
def Rolling_hash(T,mod = None,base_num=None):
    
    S = [ord(i) for i in T]
    N = len(S)
    if mod is None:
        mod = 3000012541
    if base_num is None:
        base_num = 10007
    H = [0]*(N+1)

    b_inv = [1]*N
    tb = base_num
    tb_inv = 1
    base_inv = pow(base_num,mod-2,mod)
    tH = S[0]
    H[1] = tH
    
    for i in range(N-1):
        tH = (tH + S[i+1]*tb)%mod
        H[i+2] = tH
        
        tb = (tb*base_num)%mod
        
        tb_inv *= base_inv
        tb_inv %= mod
        b_inv[i+1] = tb_inv
    
    return b_inv,H
mod = 3000012541
ans = 0
N = len(S)
b_inv,T = Rolling_hash(S)
b_inv2,T2 = Rolling_hash(S,mod,base_num=2)
for _ in range(M):
    C = list(input())
    n = len(C)
    _,hC = Rolling_hash(C)
    _,hC2 = Rolling_hash(C,mod,base_num=2)
    target = hC[n] - hC[0]
    target2 = hC2[n] - hC2[0]
    for i in range(N-n+1):
        if target == (b_inv[i]*(T[i+n] - T[i]))%mod:
            if target2 == (b_inv2[i]*(T2[i+n] - T2[i]))%mod:
                ans += 1
print(ans)
0