結果

問題 No.430 文字列検索
ユーザー AEn
提出日時 2022-12-10 17:08:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 593 ms / 2,000 ms
コード長 902 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 82,120 KB
実行使用メモリ 183,368 KB
最終ジャッジ日時 2024-11-10 01:02:57
合計ジャッジ時間 5,833 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

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