結果

問題 No.430 文字列検索
ユーザー titiatitia
提出日時 2023-04-18 20:54:22
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 544 ms / 2,000 ms
コード長 797 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 47,708 KB
最終ジャッジ日時 2024-11-10 01:06:13
合計ジャッジ時間 6,785 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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