結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-08-24 10:36:46 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,239 bytes |
| コンパイル時間 | 304 ms |
| コンパイル使用メモリ | 12,544 KB |
| 実行使用メモリ | 16,128 KB |
| 最終ジャッジ日時 | 2024-11-10 01:12:24 |
| 合計ジャッジ時間 | 3,620 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | AC * 1 TLE * 1 -- * 12 |
ソースコード
def rolling_hash(text: str, pattern: str) -> int:
a = 31
h = 998244353
counter = 0
text_length, pattern_length = len(text), len(pattern)
text_hash = pattern_hash = 0
# テキストよりもpatternの方が長い
if text_length < pattern_length:
return 0
# a^l を先に計算
a_l = 1
for _ in range(pattern_length):
a_l = (a_l * a) % h
# 最初のハッシュを計算する
for cursor in range(pattern_length):
text_hash = (a * text_hash + ord(text[cursor])) % h
pattern_hash = (a * pattern_hash + ord(pattern[cursor])) % h
for i in range(text_length - pattern_length + 1):
if text_hash == pattern_hash:
counter += 1
# 更新
if i < text_length - pattern_length:
text_hash = (text_hash * a - a_l * ord(text[i]) + ord(text[i + pattern_length])) % h
if text_hash < 0:
text_hash += h
return counter
def main():
S = input()
M = int(input())
ans = 0
for _ in range(M):
C = input()
ans += rolling_hash(S, C)
print(ans)
if __name__ == "__main__":
main()