結果
問題 |
No.430 文字列検索
|
ユーザー |
![]() |
提出日時 | 2023-02-21 10:09:30 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 454 ms / 2,000 ms |
コード長 | 922 bytes |
コンパイル時間 | 3,402 ms |
コンパイル使用メモリ | 72,036 KB |
最終ジャッジ日時 | 2025-02-10 19:39:04 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 |
ソースコード
#include <iostream> #include <vector> using namespace std; #define B1 100000007 #define B2 1000000007 int rolling_hash(string S, string c) { int ans=0; unsigned long long pow_B1=1, pow_B2=1; for(int k=0; k<c.length(); k++) pow_B1*=B1, pow_B2*=B2; unsigned long long ch1=0, ch2=0, sh1=0, sh2=0; for(int k=0; k<c.length(); k++) { ch1=ch1*B1+c[k]; ch2=ch2*B2+c[k]; sh1=sh1*B1+S[k]; sh2=sh2*B2+S[k]; } for(int k=0; k+c.length()<=S.length(); k++) { if(ch1==sh1&&ch2==sh2) ans++; sh1=sh1*B1+S[k+c.length()]-S[k]*pow_B1; sh2=sh2*B2+S[k+c.length()]-S[k]*pow_B2; } return ans; } int main() { string S; cin >> S; int M; cin >> M; vector<string> C(M); for(int i=0; i<M; i++) cin >> C[i]; int ans=0; for(int i=0; i<M; i++) ans+=rolling_hash(S, C[i]); cout << ans << '\n'; return 0; }