結果

問題 No.430 文字列検索
ユーザー IgrmiIgrmi
提出日時 2021-10-28 02:55:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 794 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 126,856 KB
最終ジャッジ日時 2024-04-16 12:55:39
合計ジャッジ時間 2,468 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,608 KB
testcase_01 AC 167 ms
126,856 KB
testcase_02 AC 95 ms
76,620 KB
testcase_03 AC 95 ms
76,544 KB
testcase_04 AC 42 ms
52,096 KB
testcase_05 AC 41 ms
52,096 KB
testcase_06 AC 42 ms
52,096 KB
testcase_07 AC 42 ms
52,096 KB
testcase_08 AC 134 ms
124,416 KB
testcase_09 AC 44 ms
53,248 KB
testcase_10 AC 59 ms
67,584 KB
testcase_11 AC 117 ms
82,432 KB
testcase_12 AC 113 ms
82,944 KB
testcase_13 AC 119 ms
82,944 KB
testcase_14 AC 110 ms
79,376 KB
testcase_15 AC 110 ms
77,696 KB
testcase_16 AC 99 ms
76,800 KB
testcase_17 AC 105 ms
76,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

Base = 1676943010
MOD = 2147483647
S = input()
Q = int(input())
Count = [dict() for _ in range(11)]
for i in range(1, min(10, len(S)) + 1):
    SHash = 0
    B = 1
    for j in range(i - 1, -1, -1):
        SHash += ord(S[j]) * B
        SHash %= MOD
        B *= Base
        B %= MOD
    Count[i][SHash] = 1
    for j in range(len(S) - i):
        SHash = ((SHash * Base - ord(S[j]) * B) + ord(S[i + j])) % MOD
        if SHash in Count[i]: Count[i][SHash] += 1
        else: Count[i][SHash] = 1
Ans = 0
for _ in range(Q):
    T = input()
    if len(S) < len(T): continue
    THash = 0
    B = 1
    for i in range(len(T) - 1, -1, -1):
        THash += ord(T[i]) * B
        THash %= MOD
        B *= Base
        B %= MOD
    if THash in Count[len(T)]: Ans += Count[len(T)][THash]
print(Ans)
0