結果

問題 No.430 文字列検索
ユーザー 👑 tatyamtatyam
提出日時 2019-09-12 23:28:05
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 685 ms / 2,000 ms
コード長 3,288 bytes
コンパイル時間 2,854 ms
コンパイル使用メモリ 219,044 KB
実行使用メモリ 7,960 KB
最終ジャッジ日時 2023-09-15 14:28:48
合計ジャッジ時間 10,225 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,336 KB
testcase_01 AC 685 ms
7,960 KB
testcase_02 AC 645 ms
7,928 KB
testcase_03 AC 605 ms
7,932 KB
testcase_04 AC 4 ms
7,208 KB
testcase_05 AC 4 ms
7,276 KB
testcase_06 AC 4 ms
7,396 KB
testcase_07 AC 4 ms
7,272 KB
testcase_08 AC 9 ms
7,748 KB
testcase_09 AC 4 ms
7,264 KB
testcase_10 AC 5 ms
7,480 KB
testcase_11 AC 656 ms
7,856 KB
testcase_12 AC 663 ms
7,928 KB
testcase_13 AC 674 ms
7,928 KB
testcase_14 AC 672 ms
7,924 KB
testcase_15 AC 658 ms
7,800 KB
testcase_16 AC 626 ms
7,860 KB
testcase_17 AC 668 ms
7,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}

0