結果

問題 No.430 文字列検索
ユーザー proriboneproribone
提出日時 2020-08-12 15:40:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 269 ms / 2,000 ms
コード長 975 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 107,560 KB
最終ジャッジ日時 2024-04-18 00:42:12
合計ジャッジ時間 3,920 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
55,424 KB
testcase_01 AC 269 ms
106,536 KB
testcase_02 AC 193 ms
86,144 KB
testcase_03 AC 214 ms
86,144 KB
testcase_04 AC 53 ms
55,680 KB
testcase_05 AC 48 ms
55,808 KB
testcase_06 AC 46 ms
55,424 KB
testcase_07 AC 52 ms
55,936 KB
testcase_08 AC 224 ms
107,560 KB
testcase_09 AC 57 ms
62,336 KB
testcase_10 AC 76 ms
77,380 KB
testcase_11 AC 209 ms
90,164 KB
testcase_12 AC 210 ms
90,112 KB
testcase_13 AC 221 ms
90,220 KB
testcase_14 AC 217 ms
87,168 KB
testcase_15 AC 202 ms
85,888 KB
testcase_16 AC 217 ms
86,016 KB
testcase_17 AC 201 ms
86,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class RollingHash:
    def __init__(self,S,base,M):
        self.N=len(S)
        self.M=M
        self.H=[0]*(self.N+1)
        self.B=[0]*(self.N+1)
        self.B[0]=1
        for i in range(self.N):
            self.B[i+1]=self.B[i]*base%M
            self.H[i+1]=(self.H[i]*base+(ord(S[i])-64))%M
    def get(self,l,r):
        return (self.H[r]-self.H[l]*self.B[r-l]+self.M)%self.M
    def getall(self,L):
        arr=[]
        for i in range(L,self.N+1):
            arr.append(self.get(i-L,i))
        return arr

def Hash(S,base,M):
    H=0
    for c in S:
        H=(H*base+ord(c)-64)%M
    return H
    
import random
from collections import Counter

S=input()
M=int(input())
li=[[] for i in range(11)]
for i in range(M):
    C=input()
    li[len(C)].append(C)

MOD=2**61-1
base=random.randrange(100,MOD)
RH=RollingHash(S,base,MOD)
ans=0
for i in range(11):
    Count=Counter(RH.getall(i))
    for C in li[i]:
        ans+=Count.get(Hash(C,base,MOD),0)
print(ans)
0