結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2016-10-02 22:55:12 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,929 bytes |
| コンパイル時間 | 1,536 ms |
| コンパイル使用メモリ | 178,780 KB |
| 実行使用メモリ | 11,128 KB |
| 最終ジャッジ日時 | 2024-11-10 00:02:34 |
| 合計ジャッジ時間 | 4,869 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | AC * 1 TLE * 1 -- * 12 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
typedef vector<int> vi;
typedef long long ll;
struct RollingHash {
static const ll mo0 = 1000000007, mo1 = 1000000009;
static ll mul0, mul1;
static const ll add0 = 1000010007, add1 = 1003333331;
static vector<ll> pmo[2];
vector<int> s; int l; vector<ll> hash_[2];
void init(vector<int> s) {
this->s = s; l = s.size(); int i, j;
hash_[0] = hash_[1] = vector<ll>(1, 0);
if (!mul0) mul0 = 10009 + (((ll)&mul0) >> 5) % 259, mul1 = 10007 + (((ll)&mul1) >> 5) % 257;
if (pmo[0].empty()) pmo[0].push_back(1), pmo[1].push_back(1);
for (int i = 0; i < l; i++) hash_[0].push_back((hash_[0].back()*mul0 + add0 + s[i]) % mo0);
for (int i = 0; i < l; i++) hash_[1].push_back((hash_[1].back()*mul1 + add1 + s[i]) % mo1);
}
pair<ll, ll> hash(int l, int r) { // s[l..r]
if (l>r) return make_pair(0, 0);
while (pmo[0].size()<r + 2)
pmo[0].push_back(pmo[0].back()*mul0%mo0), pmo[1].push_back(pmo[1].back()*mul1%mo1);
return make_pair((hash_[0][r + 1] + (mo0 - hash_[0][l] * pmo[0][r + 1 - l] % mo0)) % mo0,
(hash_[1][r + 1] + (mo1 - hash_[1][l] * pmo[1][r + 1 - l] % mo1)) % mo1);
}
};
vector<ll> RollingHash::pmo[2]; ll RollingHash::mul0, RollingHash::mul1;
//-----------------------------------------------------------------
string S;
int M;
string C[5010];
//-----------------------------------------------------------------
int main() {
cin >> S >> M;
rep(i, 0, M) cin >> C[i];
RollingHash HashS;
vi VS(S.length());
rep(i, 0, S.length()) VS[i] = S[i] - 'a';
HashS.init(VS);
int ans = 0;
rep(i, 0, M) {
RollingHash HashC;
vi VC(C[i].length());
rep(j, 0, C[i].length()) VC[j] = C[i][j] - 'a';
HashC.init(VC);
rep(j, 0, S.length() - (C[i].length() - 1)) {
auto s = HashS.hash(j, j + C[i].length() - 1);
auto c = HashC.hash(0, C[i].length() - 1);
if (s == c) ans++;
}
}
cout << ans << endl;
}
はまやんはまやん