結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-08-10 17:43:32 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,679 ms / 2,000 ms |
| コード長 | 2,467 bytes |
| コンパイル時間 | 2,679 ms |
| コンパイル使用メモリ | 247,612 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-10 01:12:23 |
| 合計ジャッジ時間 | 17,830 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
const int64_t inf = 1e18;
// const int64_t mod = 998244353;
const std::uint64_t hashmod = 0x1fffffffffffffff;
const std::uint64_t base = chrono::duration_cast<chrono::microseconds>(chrono::system_clock::now().time_since_epoch()).count() % hashmod;
const std::uint64_t mdf = uint64_t(1e18) + 1;
template<typename T>
struct rolling_hash {
vector<uint64_t> hash, p;
int64_t n;
static constexpr uint64_t mask(int64_t a){ return (1ull << a) - 1; }
inline uint64_t mul(uint64_t a, uint64_t b) const {
__uint128_t ans = __uint128_t(a) * b;
ans = (ans >> 61) + (ans & hashmod);
if(ans >= hashmod) ans -= hashmod;
return ans;
}
rolling_hash(const T &a) {
n = a.size();
hash.assign(n + 1, 0);
p.assign(n + 1, 0);
p[0] = 1;
for(int64_t i = 0; i < n; i++) {
p[i + 1] = mul(p[i], base);
hash[i + 1] = mul(hash[i], base) + (mdf + (uint64_t)a[i]);
if(hash[i + 1] >= hashmod) hash[i + 1] -= hashmod;
}
}
// get [l, r)
uint64_t get(int64_t l, int64_t r) const {
assert(0 <= l && l <= n);
assert(0 <= r && r <= n);
uint64_t ret = hash[r] + hashmod - mul(hash[l], p[r - l]);
if(ret >= hashmod) ret -= hashmod;
return ret;
}
int64_t LCP(const rolling_hash &b, int64_t l1, int64_t r1, int64_t l2, int64_t r2) {
int64_t len = min(r1 - l1, r2 - l2);
int64_t ok = -1;
int64_t ng = len + 1;
while(ng - ok > 1) {
int64_t mid = (ok + ng) / 2;
if(get(l1, l1 + mid) == b.get(l2, l2 + mid)) ok = mid;
else ng = mid;
}
return ok;
}
uint64_t connect(uint64_t hash1, uint64_t hash2, int64_t hash2len) const {
uint64_t ret = mul(hash1, p[hash2len]) + hash2;
if(ret >= hashmod) ret -= hashmod;
return ret;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
int m;
cin >> m;
long long ans = 0;
rolling_hash h(s);
for (int i = 0; i < m; i++) {
string t;
cin >> t;
if (t.size() > s.size()) continue;
rolling_hash hh(t);
uint64_t hash = hh.get(0, t.size());
for (int j = 0; j <= (int)s.size()-t.size(); j++) {
if (hash == h.get(j, j + t.size())) ans++;
}
}
cout << ans << endl;
}