結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
👑 tatyam
|
| 提出日時 | 2019-09-12 23:28:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 740 ms / 2,000 ms |
| コード長 | 3,288 bytes |
| コンパイル時間 | 2,486 ms |
| コンパイル使用メモリ | 222,204 KB |
| 最終ジャッジ日時 | 2025-01-07 17:40:56 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
const int bases[64] = {257,262,266,275,276,281,285,290,296,302,306,310,311,313,323,333,344,345,350,357,367,370,373,402,423,425,431,440,442,443,454,457,458,462,471,478,481,487,489,492,499,501,502,503,506,514,524,532,535,541,550,552,557,559,562,563,567,570,571,580,592,597,604,612};
const ull mod = 0x1fffffffffffffff, base = bases[chrono::duration_cast<chrono::microseconds>(chrono::system_clock::now().time_since_epoch()).count() & 63];
ull mem[50000][10];
struct RollingHash {
vector<ull> hashed, power;
static constexpr ull mask(int a) { return (1ull << a) - 1; }
inline ull mul(ull a, ull b) const {
//*
__uint128_t ans = __uint128_t(a) * b;
/*/
// without __uint128_t
ull a31 = a >> 31, b31 = b >> 31;
a &= mask(31);
b &= mask(31);
ull x = a * b31 + b * a31;
ull ans = (a31 * b31 << 1) + (x >> 30) + ((x & mask(30)) << 31) + a * b;
//*/
ans = (ans >> 61) + (ans & mask(61));
if(ans >= mod) ans -= mod;
return ans;
}
RollingHash(const string &s) {
ll n = s.size();
hashed.assign(n + 1, 0);
power.assign(n + 1, 0);
power[0] = 1;
for(ll i = 0; i < n; 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(ll l, ll r) const {
if(mem[l][r - l - 1] != mod) return mem[l][r - l - 1];
ull ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
if(ret >= mod) ret -= mod;
return mem[l][r - l - 1] = ret;
}
ull connect(ull h1, ll h2, ll h2len) const {
ull ret = mul(h1, power[h2len]) + h2;
if(ret >= mod) ret -= mod;
return ret;
}
void connect(const string &s){
ll n = hashed.size() - 1, m = s.size();
hashed.resize(n + m + 1);
power.resize(n + m + 1);
for(ll i = n; i < n + m; i++) {
power[i + 1] = mul(power[i], base);
hashed[i + 1] = mul(hashed[i], base) + s[i - n];
if(hashed[i + 1] >= mod) hashed[i + 1] -= mod;
}
}
ll LCP(const RollingHash &b, ll l1, ll r1, ll l2, ll r2) {
ll len = min(r1 - l1, r2 - l2);
ll low = -1, high = len + 1;
while(high - low > 1) {
ll mid = (low + high) / 2;
if(get(l1, l1 + mid) == b.get(l2, l2 + mid)) low = mid;
else high = mid;
}
return low;
}
};
signed main() {
for(auto& i : mem) for(auto& j : i) j = mod;
string s;
int m;
cin >> s >> m;
RollingHash rh(s);
int ans = 0;
for(ll i = 0; i < m; i++) {
string t;
cin >> t;
__uint128_t rh2 = 0;
for(auto i : t) {
rh2 *= base;
rh2 = (rh2 >> 61) + (rh2 & RollingHash::mask(61));
rh2 += i;
if(rh2 >= mod) rh2 -= mod;
}
for(ll i = t.size(); i < s.size() + 1; i++) ans += rh.get(i - t.size(), i) == rh2;
}
cout << ans << endl;
}
tatyam