結果

問題 No.430 文字列検索
ユーザー AEnAEn
提出日時 2022-12-10 17:08:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 684 ms / 2,000 ms
コード長 902 bytes
コンパイル時間 2,602 ms
コンパイル使用メモリ 82,132 KB
実行使用メモリ 183,524 KB
最終ジャッジ日時 2024-04-23 00:38:59
合計ジャッジ時間 7,243 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,732 KB
testcase_01 AC 684 ms
183,524 KB
testcase_02 AC 387 ms
79,144 KB
testcase_03 AC 383 ms
79,168 KB
testcase_04 AC 41 ms
55,116 KB
testcase_05 AC 41 ms
54,292 KB
testcase_06 AC 40 ms
54,680 KB
testcase_07 AC 40 ms
55,560 KB
testcase_08 AC 651 ms
182,044 KB
testcase_09 AC 48 ms
62,852 KB
testcase_10 AC 111 ms
86,924 KB
testcase_11 AC 536 ms
97,412 KB
testcase_12 AC 531 ms
97,408 KB
testcase_13 AC 541 ms
97,408 KB
testcase_14 AC 494 ms
90,524 KB
testcase_15 AC 455 ms
86,012 KB
testcase_16 AC 399 ms
79,452 KB
testcase_17 AC 403 ms
79,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


S = input()
S = [ord(i)-96 for i in S]

l = len(S)
b1 = 10**9+7
b2 = 10**5+7
mod = (1<<61)-1
p1, p2, h1, h2 = [1]*(l+1),[1]*(l+1),[0]*(l+1),[0]*(l+1)
for i in range(l):
    p1[i+1] = (p1[i]*b1)%mod
    p2[i+1] = (p2[i]*b2)%mod
    h1[i+1] = (h1[i]*b1+S[i])%mod
    h2[i+1] = (h2[i]*b2+S[i])%mod

def gethash(l,r):
    """2つの基数でのハッシュ値を返す"""
    a = (h1[r]-h1[l]*p1[r-l])%mod
    b = (h2[r]-h2[l]*p2[r-l])%mod
    return a, b

d = defaultdict(int)
for i in range(l):
    for j in range(1,11):
        if i+j<=l:
            a, b = gethash(i,i+j)
            d[str(a)+'_'+str(b)] += 1
M = int(input())
res = 0
for i in range(M):
    C = input()
    C = [ord(i)-96 for i in C]
    hash1, hash2 = 0,0
    for num in C:
        hash1 = (hash1*b1+num)%mod
        hash2 = (hash2*b2+num)%mod
    res += d[str(hash1)+'_'+str(hash2)]
print(res)

0