結果

問題 No.430 文字列検索
ユーザー 👑 tatyam
提出日時 2019-09-12 23:15:53
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,540 bytes
コンパイル時間 12,622 ms
コンパイル使用メモリ 272,780 KB
最終ジャッジ日時 2025-01-07 17:40:09
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 5 TLE * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#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;
}
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 {
        ull ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
        if(ret >= mod) ret -= mod;
        return ret;
    }
};
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    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;
}

0