結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
kyo1
|
| 提出日時 | 2020-02-07 14:21:42 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 748 ms / 2,000 ms |
| コード長 | 1,570 bytes |
| コンパイル時間 | 1,430 ms |
| コンパイル使用メモリ | 169,372 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-10 00:41:03 |
| 合計ジャッジ時間 | 9,464 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using u64 = std::uint_fast64_t;
class RollingHash {
private:
using u64 = std::uint_fast64_t;
const u64 mod = (1ULL << 61) - 1;
std::vector<u64> hashes, pows;
inline u64 add(u64 a, u64 b) const {
a += b;
if (a >= mod) a -= mod;
return a;
}
inline u64 sub(u64 a, u64 b) const {
if (a < b) a += mod;
a -= b;
return a;
}
inline u64 mul(u64 a, u64 b) const {
u64 ah = a >> 31, al = a & ((1ULL << 31) - 1), bh = b >> 31,
bl = b & ((1ULL << 31) - 1), m = ah * bl + al * bh,
v = (ah * bh << 1) + (m >> 30) + ((m & ((1ULL << 30) - 1)) << 31) +
al * bl;
v = (v & ((1ULL << 61) - 1)) + (v >> 61);
return v;
}
public:
template <typename T>
RollingHash(const T &S, u64 base = 10007)
: hashes(S.size() + 1, 0), pows(S.size() + 1, 0) {
pows[0] = 1;
for (size_t i = 0; i < S.size(); i++) {
pows[i + 1] = mul(pows[i], base);
hashes[i + 1] = add(mul(hashes[i], base), S[i]);
}
}
// S[r, l)
u64 slice(int l, int r) const {
return sub(hashes[r], mul(hashes[l], pows[r - l]));
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string S;
cin >> S;
RollingHash rhs(S);
int M;
cin >> M;
int res = 0;
for (int i = 0; i < M; i++) {
string m;
cin >> m;
RollingHash rhm(m);
for (int i = 0; i + (int)m.size() < (int)S.size() + 1; i++) {
if (rhs.slice(i, i + (int)m.size()) == rhm.slice(0, (int)m.size())) res++;
}
}
cout << res << '\n';
return 0;
}
kyo1