結果

問題 No.430 文字列検索
ユーザー rkato5680rkato5680
提出日時 2020-09-13 13:35:08
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 733 bytes
コンパイル時間 104 ms
コンパイル使用メモリ 10,944 KB
実行使用メモリ 12,792 KB
最終ジャッジ日時 2023-09-02 21:35:09
合計ジャッジ時間 3,757 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,196 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

def rolling_hash2(S, s, b=10, mod=10**9+7, d='a'):
    if d == 'a':
        d = {chr(i): i - 97 for i in range(97,123)}
    elif d == 'A':
        d = {chr(i): i - 65 for i in range(65,91)}
    
    n = len(s)
    h = sum([d[S[i]]*pow(b,n-i-1,mod) for i in range(n)]) % mod
    h0 = sum([d[s[i]]*pow(b,n-i-1,mod) for i in range(n)]) % mod
    if h == h0: res = 1
    else: res = 0
    for i in range(1,len(S)-n+1):
        h -= d[S[i-1]]*b**(n-1)
        h *= b
        h += d[S[i+n-1]]
        h %= mod
        if h == h0:
            res += 1
    
    return res

S = input()
M = int(input())
ans = 0
for i in range(M):
  s = input()
  if len(s)==1:
    ans += S.count(s)
  else:
    ans += rolling_hash2(S,s,d='A')
    
print(ans)
0