結果

問題 No.430 文字列検索
ユーザー titia
提出日時 2023-04-18 06:47:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 797 bytes
コンパイル時間 562 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 118,756 KB
最終ジャッジ日時 2024-11-10 01:06:11
合計ジャッジ時間 3,433 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter

S=input()

# Rolling Hash

LEN=len(S)

p=27 # 文字の種類
mod=67280421310721 # Hashがぶつからない, pと互いに素な数を適当に指定

TABLE=[0] # Rolling Hashのテーブル. 最初は0

for i in range(LEN):
    TABLE.append((p*TABLE[-1]%mod+ord(S[i])-64)%mod) # テーブルを埋める

def hash_ij(i,j): # [i,j)のハッシュ値を求める
    return (TABLE[j]-TABLE[i]*pow(p,j-i,mod))%mod

LIST=[[] for i in range(11)]

for i in range(1,11):
    for j in range(LEN-i+1):
        LIST[i].append(hash_ij(j,j+i))

for i in range(1,11):
    LIST[i]=Counter(LIST[i])

ANS=0
Q=int(input())
for tests in range(Q):
    c=input()
    now=0
    for i in range(len(c)):
        now=(p*now%mod+ord(c[i])-64)%mod

    ANS+=LIST[len(c)][now]

print(ANS)
0