結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-11-14 02:50:16 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 604 ms / 2,000 ms |
| コード長 | 1,074 bytes |
| コンパイル時間 | 1,362 ms |
| コンパイル使用メモリ | 171,740 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-10 00:36:49 |
| 合計ジャッジ時間 | 7,851 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<ull mod = 1000000007>
struct RollingHash {
vector<ull> hashs, power;
RollingHash() {}
RollingHash(const string& s, ull base = 10007) {
int sz = s.size();
hashs.assign(sz + 1, 0);
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++) {
power[i + 1] = power[i] * base % mod;
hashs[i + 1] = (hashs[i] * base) % mod + s[i];
if (hashs[i + 1] >= mod) hashs[i] -= mod;
}
}
ull get(int l, int r) const {
ull res = hashs[r] + mod - hashs[l] * power[r - l] % mod;
if (res >= mod) res -= mod;
return res;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s;
cin >> s;
int n = s.size();
int m;
cin >> m;
RollingHash<> rhs(s);
int ans = 0;
for (int i = 0; i < m; i++) {
string c;
cin >> c;
RollingHash<> rhc(c);
int cl = c.size();
int cnt = 0;
for (int i = 0; i + cl <= n; i++) {
cnt += (rhs.get(i, i + cl) == rhc.get(0, cl));
}
ans += cnt;
}
cout << ans << "\n";
return 0;
}