結果

問題 No.430 文字列検索
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-01-05 07:23:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,646 ms / 2,000 ms
コード長 1,070 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 81,604 KB
最終ジャッジ日時 2024-04-23 10:56:13
合計ジャッジ時間 16,860 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,176 KB
testcase_01 AC 1,515 ms
81,528 KB
testcase_02 AC 1,569 ms
81,604 KB
testcase_03 AC 1,560 ms
81,308 KB
testcase_04 AC 40 ms
53,436 KB
testcase_05 AC 39 ms
52,964 KB
testcase_06 AC 39 ms
52,532 KB
testcase_07 AC 38 ms
53,224 KB
testcase_08 AC 65 ms
68,196 KB
testcase_09 AC 49 ms
61,980 KB
testcase_10 AC 56 ms
65,052 KB
testcase_11 AC 1,646 ms
81,576 KB
testcase_12 AC 1,459 ms
81,304 KB
testcase_13 AC 1,457 ms
81,576 KB
testcase_14 AC 1,637 ms
81,252 KB
testcase_15 AC 1,487 ms
81,376 KB
testcase_16 AC 1,553 ms
81,596 KB
testcase_17 AC 1,569 ms
81,352 KB
権限があれば一括ダウンロードができます

ソースコード

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