結果
問題 | No.430 文字列検索 |
ユーザー | Risu_Basquiat |
提出日時 | 2020-06-20 00:25:53 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 261 ms / 2,000 ms |
コード長 | 2,235 bytes |
コンパイル時間 | 1,094 ms |
コンパイル使用メモリ | 86,936 KB |
実行使用メモリ | 128,316 KB |
最終ジャッジ日時 | 2023-09-16 16:16:12 |
合計ジャッジ時間 | 5,652 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 115 ms
73,412 KB |
testcase_01 | AC | 261 ms
127,796 KB |
testcase_02 | AC | 185 ms
85,336 KB |
testcase_03 | AC | 184 ms
85,228 KB |
testcase_04 | AC | 116 ms
73,232 KB |
testcase_05 | AC | 115 ms
73,348 KB |
testcase_06 | AC | 119 ms
73,064 KB |
testcase_07 | AC | 117 ms
73,360 KB |
testcase_08 | AC | 221 ms
128,316 KB |
testcase_09 | AC | 118 ms
73,280 KB |
testcase_10 | AC | 134 ms
80,828 KB |
testcase_11 | AC | 202 ms
92,252 KB |
testcase_12 | AC | 204 ms
92,104 KB |
testcase_13 | AC | 206 ms
92,192 KB |
testcase_14 | AC | 198 ms
88,892 KB |
testcase_15 | AC | 192 ms
86,796 KB |
testcase_16 | AC | 187 ms
85,212 KB |
testcase_17 | AC | 186 ms
85,128 KB |
ソースコード
import math import sys from collections import Counter#文字列を個数カウント辞書に、 def input(): x=sys.stdin.readline() return x[:-1] if x[-1]=="\n" else x def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65 import random class rh:#base,max_n,convertを指定すること def __init__(self,base,max_n):#baseとmaxnを指定すること。baseは偶数(modが奇数) self.mod=random.randrange(1<<54-1,1<<55-1,2)#奇数、衝突させられないよう乱数で生成 self.base=int(base*0.5+0.5)*2#偶数,英小文字なら26とか self.max_n=max_n self.pows1=[1]*(max_n+1) cur1=cur2=1 for i in range(1,max_n+1): cur1=cur1*base%self.mod self.pows1[i]=cur1 def convert(self,c): return alp2num(c,True) def get(self, s):#特定の文字のhash. O(|s|) n=len(s) mod=self.mod si=[self.convert(ss) for ss in s] h1=sum([si[i]*self.pows1[n-1-i]%mod for i in range(n)])%mod return h1 def get_roll(self, s, k):#ローリングハッシュ,特定の文字の中から長さkのhashをすべて得る,O(|s|) n=len(s) si=[self.convert(ss) for ss in s] mod=self.mod h1=sum([si[i]*self.pows1[k-1-i]%mod for i in range(k)])%mod hs=[0]*(n-k+1) hs[0]=h1 mbase1=self.pows1[k] base=self.base for i in range(1,n-k+1): front=si[i-1];back=si[i+k-1] h1=(h1*base-front*mbase1+back)%mod hs[i]=h1 return hs def rc(s,k,rh): hs=rh.get_roll(s,k) cnt=set() for i, h in enumerate(hs): if h in cnt: return s[i:i+k] else: cnt.add(h) return 0 def main(): s=input() M=int(input()) rhi=rh(26,len(s)) l=[] for k in range(1,min(len(s)+1,11)): l.append(Counter(rhi.get_roll(s,k))) ans=0 for m in range(M): c=input() k=len(c) if k>len(s): continue ch=rhi.get(c) #print(len(l)) if ch in l[k-1]: ans+=l[k-1][ch] print(ans) if __name__ == "__main__": main() #print(pows1)