結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
17,696 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 #

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)
for _ in range(M):
    C = list(input())
    n = len(C)
    _,hC = Rolling_hash(C)
    target = hC[n] - hC[0]
    for i in range(N-n+1):
        if target == (b_inv[i]*(T[i+n] - T[i]))%mod:
            ans += 1
print(ans)
0