結果
問題 |
No.430 文字列検索
|
ユーザー |
|
提出日時 | 2020-08-13 15:12:08 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 237 ms / 2,000 ms |
コード長 | 2,058 bytes |
コンパイル時間 | 1,519 ms |
コンパイル使用メモリ | 181,120 KB |
実行使用メモリ | 27,136 KB |
最終ジャッジ日時 | 2024-11-10 00:45:57 |
合計ジャッジ時間 | 2,937 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (int)(n); i ++) #define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i) using namespace std; using ll = long long; using PL = pair<ll,ll>; using P = pair<int,int>; constexpr int INF = 1000000000; constexpr long long HINF = 1000000000000000; constexpr long long MOD = 1000000007;// = 998244353; constexpr double EPS = 1e-4; constexpr double PI = 3.14159265358979; using u64 = unsigned long long; constexpr u64 M = 2305843009213693951; // 2^61 - 1 constexpr u64 MASK31 = 2147483647; // 2^31 - 1 constexpr u64 POSITIVIZER = 4*M; u64 CalcMod(u64 x) { u64 res = (x >> 61) + (x&M); if (res >= M) res -= M; return res; } u64 mul(u64 x,u64 y) { u64 au = (x >> 31),ad = (x&MASK31),bu = (y >> 31),bd = (y&MASK31); u64 m = au * bd + ad * bu; u64 mu = (m >> 31),md = (m&MASK31); u64 ans = 2*au*bu + 2*mu + (md << 31) + ad*bd; return CalcMod(ans); } struct RollingHash { static u64 B; int N; vector<u64> hash,power; RollingHash(const string &s) { N = (int)s.size(); hash.resize(N + 1,0); power.resize(N + 1,0); hash[0] = 0; power[0] = 1; for (int i = 0; i < (int)s.size(); ++i) { hash[i + 1] = CalcMod(mul(hash[i],B) + s[i]); power[i + 1] = mul(power[i],B); } } u64 get() {return hash[N];} u64 get(int k) {return hash[k];} u64 get(int l,int r) {return CalcMod(hash[r] + POSITIVIZER - mul(hash[l],power[r - l]));} }; mt19937_64 mt; uniform_int_distribution<unsigned long long> rng(2,M - 2); unsigned long long RollingHash::B = rng(mt); int main() { string S; cin >> S; RollingHash rh(S); map<u64,ll> mp; for (int l = 0;l < S.size(); ++l) { for (int r = l + 1;r <= min(l + 10,(int)S.size()); ++r) { u64 x = rh.get(l,r); mp[x] ++; } } ll ans = 0; int M; cin >> M; rep(i,M) { string a; cin >> a; RollingHash rha(a); ans += mp[rha.get()]; } cout << ans << '\n'; return 0; }