結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-11-12 22:00:08 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 279 ms / 2,000 ms |
| コード長 | 1,768 bytes |
| コンパイル時間 | 2,172 ms |
| コンパイル使用メモリ | 202,608 KB |
| 最終ジャッジ日時 | 2025-01-25 16:44:53 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int64_t random_number(int64_t n) {
mt19937_64 mt(std::chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<int64_t> dist(0, n - 1);
return dist(mt);
}
const int64_t MOD = ((int64_t)1 << 61) - 1;
const int64_t MASK30 = ((int64_t)1 << 30) - 1;
const int64_t MASK31 = ((int64_t)1 << 31) - 1;
const int64_t MASK61 = ((int64_t)1 << 61) - 1;
int64_t base = random_number(MOD);
int64_t mod(int64_t x) {
int64_t res = (x >> 61) + (x & MASK61);
if (res >= MOD) res -= MOD;
return res;
}
int64_t mul(int64_t a, int64_t b) {
int64_t au = (a >> 31), ad = (a & MASK31);
int64_t bu = (b >> 31), bd = (b & MASK31);
int64_t c = (au * bd + ad * bu);
int64_t cu = (c >> 30), cd = (c & MASK30);
return mod(au * bu * 2 + cu + (cd << 31) + ad * bd);
}
int64_t add(int64_t a, int64_t b) {
return mod(a + b);
}
int64_t sub(int64_t a, int64_t b) {
int64_t res = a - b;
if (res < 0) res += MOD;
return res;
}
int main() {
string S;
int M;
cin >> S >> M;
map<int64_t, int> mp;
for (int k = 1; k <= 10; ++k) {
int64_t h = 0, t = 1;
for (int i = 0; i < k; ++i) {
h = add(mul(h, base), S[i]);
t = mul(t, base);
}
for (int i = 0; i + k <= (int)S.size(); ++i) {
mp[h]++;
if (i + k < (int)S.size()) {
h = sub(add(mul(h, base), S[i + k]), mul(S[i], t));
}
}
}
int ans = 0;
for (int q = 0; q < M; ++q) {
string C;
cin >> C;
int64_t ch = 0;
for (auto&& c : C) {
ch = add(mul(ch, base), c);
}
ans += mp[ch];
}
cout << ans << endl;
return 0;
}