結果
問題 | No.430 文字列検索 |
ユーザー |
|
提出日時 | 2024-06-22 20:49:40 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,526 bytes |
コンパイル時間 | 1,558 ms |
コンパイル使用メモリ | 168,220 KB |
実行使用メモリ | 10,624 KB |
最終ジャッジ日時 | 2024-11-10 01:11:43 |
合計ジャッジ時間 | 4,717 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 4 |
other | AC * 1 TLE * 1 -- * 12 |
ソースコード
// https://yukicoder.me/problems/no/430 #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; #define rep0(i,n) for(int i=0; i<n; ++i) #define rep1(i,n) for(int i=1; i<=n; ++i) ll powpow(ll x, ll n){ if(n==0) return 1; ll tmp = powpow(x, n/2); if(n % 2 == 1) return (tmp * tmp * x); else return (tmp * tmp); } ll mod = 998244353; int base = 26; ll powpowm(ll x, ll n){ if(n==0) return 1; ll tmp = powpow(x, n/2); if(n % 2 == 1) return (((tmp * tmp) % mod) * x) % mod; else return (tmp * tmp) % mod; } ll base_n[11]; int RollingHashMatch(string text, string pattern){ int ans = 0; int t_len = text.length(); int p_len = pattern.length(); ll t_hash = 0; ll p_hash = 0; rep0(i, p_len){ p_hash += base_n[p_len-1-i] * (ll)(pattern[i]-'A'); p_hash %= mod; t_hash += base_n[p_len-1-i] * (ll)(text[i]-'A'); t_hash %= mod; } if(p_hash == t_hash) ans += 1; // 先頭文字でforを回してt_hashを更新していく rep1(i, t_len - p_len){ ll new_hash = base * t_hash; new_hash -= base_n[p_len] * (text[i-1]-'A'); new_hash += (text[i + p_len - 1] - 'A'); new_hash %= mod; if(new_hash < 0) new_hash += mod; t_hash = new_hash; if(p_hash == t_hash) ans += 1; } return ans; } int main(){ string S; cin >> S; int M; cin >> M; rep0(i, 11) base_n[i] = powpowm(base, i); int ans = 0; rep1(i, M){ string C; cin >> C; ans += RollingHashMatch(S, C); } cout << ans << endl; }