結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
👑 tatyam
|
| 提出日時 | 2019-09-12 23:23:21 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 820 ms / 2,000 ms |
| コード長 | 1,624 bytes |
| コンパイル時間 | 2,799 ms |
| コンパイル使用メモリ | 219,188 KB |
| 最終ジャッジ日時 | 2025-01-07 17:40:36 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned;
using ull = unsigned long long;
constexpr ull mod = 0x1fffffffffffffff, base = 257;
constexpr ull mask(int a) { return (1ull << a) - 1; }
inline constexpr ull mul(ull a, ull b) {
__uint128_t ans = __uint128_t(a) * b;
ans = (ans >> 61) + (ans & mask(61));
if(ans >= mod) ans -= mod;
return ans;
}
ull mem[50000][11];
struct RollingHash {
ull hashed[50001], power[50001];
RollingHash(const string &s) {
power[0] = 1;
for(uint i = 0; i < s.size(); i++) {
power[i + 1] = mul(power[i], base);
hashed[i + 1] = mul(hashed[i], base) + s[i];
if(hashed[i + 1] >= mod) hashed[i + 1] -= mod;
}
}
ull get(uint l, uint r) const {
if(mem[l][r - l] != mod) return mem[l][r - l];
ull ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
if(ret >= mod) ret -= mod;
return mem[l][r - l] = ret;
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
for(auto& i : mem) for(auto& j : i) j = mod;
string s;
int m;
cin >> s >> m;
RollingHash rh(s);
int ans = 0;
for(uint i = 0; i < m; i++) {
string t;
cin >> t;
__uint128_t rh2 = 0;
for(auto i : t) {
rh2 *= base;
rh2 = (rh2 >> 61) + (rh2 & mask(61));
rh2 += i;
if(rh2 >= mod) rh2 -= mod;
}
for(uint i = t.size(); i < s.size() + 1; i++) ans += rh.get(i - t.size(), i) == rh2;
}
cout << ans << endl;
}
tatyam