結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2016-10-02 23:07:47 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,733 bytes |
| コンパイル時間 | 1,413 ms |
| コンパイル使用メモリ | 172,560 KB |
| 実行使用メモリ | 11,940 KB |
| 最終ジャッジ日時 | 2024-11-10 00:03:08 |
| 合計ジャッジ時間 | 4,740 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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++)
struct RollingHash {
typedef long long int_type;
typedef pair<int_type, int_type> hash_type;
int_type base1;
int_type base2;
int_type mod1;
int_type mod2;
vector<int_type> hash1;
vector<int_type> hash2;
vector<int_type> pow1;
vector<int_type> pow2;
RollingHash() : base1(1009), base2(1007), mod1(1000000007), mod2(1000000009) {}
void init(const string &s) {
int n = s.size();
hash1.assign(n + 1, 0);
hash2.assign(n + 1, 0);
pow1.assign(n + 1, 1);
pow2.assign(n + 1, 1);
for (int i = 0; i<n; i++) {
hash1[i + 1] = (hash1[i] + s[i]) * base1 % mod1;
hash2[i + 1] = (hash2[i] + s[i]) * base2 % mod2;
pow1[i + 1] = pow1[i] * base1 % mod1;
pow2[i + 1] = pow2[i] * base2 % mod2;
}
}
hash_type get(int l, int r) {
int_type t1 = ((hash1[r] - hash1[l] * pow1[r - l]) % mod1 + mod1) % mod1;
int_type t2 = ((hash2[r] - hash2[l] * pow2[r - l]) % mod2 + mod2) % mod2;
return make_pair(t1, t2);
}
RollingHash::hash_type concat(hash_type h1, hash_type h2, int h2_len) {
return make_pair((h1.first*pow1[h2_len] + h2.first) % mod1, (h1.second*pow2[h2_len] + h2.second) % mod2);
}
};
//-----------------------------------------------------------------
string S;
int M;
string C[5010];
//-----------------------------------------------------------------
int main() {
cin >> S >> M;
rep(i, 0, M) cin >> C[i];
RollingHash HashS;
HashS.init(S);
int ans = 0;
rep(i, 0, M) {
RollingHash HashC;
HashC.init(C[i]);
rep(j, 0, S.length() - (C[i].length() - 1)) {
auto s = HashS.get(j, j + C[i].length());
auto c = HashC.get(0, C[i].length());
if (s == c) ans++;
}
}
cout << ans << endl;
}
はまやんはまやん