結果

問題 No.430 文字列検索
ユーザー KN711KN711
提出日時 2023-01-29 18:42:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,622 ms / 2,000 ms
コード長 680 bytes
コンパイル時間 649 ms
コンパイル使用メモリ 87,084 KB
実行使用メモリ 83,216 KB
最終ジャッジ日時 2023-09-12 01:17:01
合計ジャッジ時間 16,594 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
71,524 KB
testcase_01 AC 1,622 ms
82,628 KB
testcase_02 AC 1,521 ms
82,328 KB
testcase_03 AC 1,431 ms
82,596 KB
testcase_04 AC 75 ms
71,284 KB
testcase_05 AC 75 ms
71,528 KB
testcase_06 AC 75 ms
71,216 KB
testcase_07 AC 76 ms
71,316 KB
testcase_08 AC 99 ms
81,264 KB
testcase_09 AC 79 ms
71,212 KB
testcase_10 AC 89 ms
76,352 KB
testcase_11 AC 1,096 ms
82,460 KB
testcase_12 AC 1,430 ms
82,596 KB
testcase_13 AC 1,433 ms
82,496 KB
testcase_14 AC 1,434 ms
82,796 KB
testcase_15 AC 1,432 ms
82,864 KB
testcase_16 AC 1,435 ms
82,492 KB
testcase_17 AC 1,536 ms
83,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

base = 30
MOD = 10**9 + 7

pw = [1]
for i in range(N):
    pw.append(pw[-1]*base % MOD)

hashtable = [0]
for i in range(N):
    hashtable.append((hashtable[-1]*base + ord(S[i])-ord("a"))%MOD)
#print(hashtable)
res = 0
for _ in range(M):
    s = input()
    LEN = len(s)
    if LEN > N:
        continue
    s_hash = [0]
    for i in range(LEN):
        s_hash.append((s_hash[-1]*base + ord(s[i])-ord("a"))%MOD)
    tmp1 = s_hash[-1]
    #print(s,tmp1)
    
    for i in range(N-LEN+1):
        tmp2 = (hashtable[LEN+i] - hashtable[i]*pw[LEN]) % MOD
        if tmp2 == tmp1:
            #print(tmp2,tmp1,i,s)
            res += 1
print(res)
0