結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
planes
|
| 提出日時 | 2024-01-21 16:43:24 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 55 ms / 2,000 ms |
| コード長 | 1,950 bytes |
| コンパイル時間 | 1,937 ms |
| コンパイル使用メモリ | 181,736 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-10 01:09:59 |
| 合計ジャッジ時間 | 2,416 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
ll INF=2e18;
class rolling_hash {
public:
using u64 = std::uint_fast64_t;
using size_type = std::uint_fast32_t;
using i128 = __int128_t;
static constexpr u64 MOD = (1uL << 61) - 1;
static constexpr u64 base = 20200213;
std::string str;
std::vector<u64> hash_, pow;
private:
static constexpr u64 mask30 = (1ul << 30) - 1;
static constexpr u64 mask31 = (1ul << 31) - 1;
u64 mul(i128 a, i128 b) const {
i128 t = a * b;
t = (t >> 61) + (t & MOD);
if(t >= MOD) return t - MOD;
return t;
}
size_type xorshift(size_type x) const {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
}
public:
rolling_hash(const rolling_hash &) = default;
rolling_hash(rolling_hash&&) = default;
rolling_hash() : str() {};
rolling_hash(const std::string & str) : str(str) {
hash_.resize(str.size() + 1, 0);
pow.resize(str.size() + 1, 1);
for(size_type i = 0; i < str.size(); i++) {
hash_[i + 1] = mul(hash_[i], base) + xorshift(str[i] + 1);
pow[i + 1] = mul(pow[i], base);
if(hash_[i + 1] >= MOD) hash_[i + 1] -= MOD;
}
}
u64 hash() const { return hash_.back(); }
u64 hash(size_type l, size_type r) const {
u64 ret = MOD + hash_[r] - mul(hash_[l], pow[r - l]);
return ret < MOD ? ret : ret - MOD;
}
size_type size() const { return str.size(); }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string S;cin>>S;
ll M;cin>>M;
vector<string> C(M);
for(ll i=0;i<M;i++) cin>>C[i];
rolling_hash hash(S);
map<ll,ll> Q;
for(ll i=0;i<M;i++) {
rolling_hash h(C[i]);
Q[h.hash(0,C[i].size())]++;
}
ll ans=0;
for(ll i=0;i<S.size();i++) {
for(ll j=0;j<10;j++) {
ll val=hash.hash(i,i+j+1);
if(Q.count(val)) ans+=Q[val];
}
}
cout<<ans<<endl;
}
planes