結果

問題 No.430 文字列検索
ユーザー Arutu_R2Arutu_R2
提出日時 2020-08-25 21:30:16
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,127 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 121,404 KB
最終ジャッジ日時 2024-04-24 04:48:55
合計ジャッジ時間 3,862 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

from collections import defaultdict

S = input()
N = len(S)
M = int(input())

base = 1007
mod = 2**61-1

hash = [0]*(N+1)
power = [1]*(N+1)
for i in range(1,N+1):
    hash[i] = (hash[i-1]*base + ord(S[i-1])) %mod
    power[i] = power[i-1]*base %mod

def RollingHash(l,r):
    return hash[r] - hash[l]*power[r-l]%mod


base2 = 2009
mod2 = 10**9+9

hash2 = [0]*(N+1)
power2 = [1]*(N+1)
for i in range(1,N+1):
    hash2[i] = (hash2[i-1]*base2 + ord(S[i-1])) %mod2
    power2[i] = power2[i-1]*base2 %mod2

def RollingHash2(l,r):
    return hash2[r] - hash2[l]*power2[r-l]%mod2

possible = [defaultdict(int) for _ in range(11)]
for i in range(10):
    for j in range(N+1-i):
        possible[i][(RollingHash(j,j+i),RollingHash2(j,j+i))] += 1



ans = 0
for i in range(M):
    C = input()
    hashC = 0
    hashC2 = 0
    for i in range(len(C)):
        hashC += ord(C[i])*base**(len(C)-1-i) %mod
        hashC %= mod
        hashC2 += ord(C[i])*base2**(len(C)-1-i) %mod2
        hashC2 %= mod2
    #if possible[len(C)][hashC] == possible2[len(C)][hashC]:
    ans += possible[len(C)][(hashC,hashC2)]
    #print(hashC)
    
print(ans)
0