結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-06-22 20:43:32 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,486 bytes |
| コンパイル時間 | 1,670 ms |
| コンパイル使用メモリ | 169,620 KB |
| 実行使用メモリ | 10,624 KB |
| 最終ジャッジ日時 | 2024-11-10 01:11:38 |
| 合計ジャッジ時間 | 5,031 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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;
}
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 += powpowm(base, p_len-1-i) * (ll)(pattern[i]-'A');
p_hash %= mod;
t_hash += powpowm(base, 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 -= powpowm(base, 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;
int ans = 0;
rep1(i, M){
string C;
cin >> C;
ans += RollingHashMatch(S, C);
}
cout << ans << endl;
}