結果

問題 No.430 文字列検索
ユーザー essence0826essence0826
提出日時 2023-09-12 00:04:46
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 680 ms / 2,000 ms
コード長 3,396 bytes
コンパイル時間 4,656 ms
コンパイル使用メモリ 259,944 KB
実行使用メモリ 44,992 KB
最終ジャッジ日時 2023-09-12 00:04:54
合計ジャッジ時間 7,747 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 680 ms
44,992 KB
testcase_02 AC 45 ms
5,168 KB
testcase_03 AC 46 ms
5,132 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 629 ms
44,468 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 26 ms
7,676 KB
testcase_11 AC 125 ms
11,508 KB
testcase_12 AC 125 ms
11,736 KB
testcase_13 AC 126 ms
11,732 KB
testcase_14 AC 100 ms
8,908 KB
testcase_15 AC 83 ms
7,436 KB
testcase_16 AC 50 ms
5,564 KB
testcase_17 AC 48 ms
5,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define BE(x) x.begin(), x.end()
using ll = long long;
template<class T> using vec = vector<T>;


class rolling_hash {

  private:
    using ull = uint64_t;
    static const ull max_a = 127ull; // set the max value of sequence to be hashed

    static constexpr ull p = (1ull<<61)-1ull;
    static const ull p_root = 37ull;

    static const int base_n = 2; // number of bases
    static ull base[base_n];
    static vector<ull> base_pow[base_n];
    vector<ull> hash[base_n];

    ull mod(ull x) {
        ull res = (x>>61) + (x&p);
        if (res >= p) res -= p;
        return res;
    }
    ull mul(ull a, ull b) {
        constexpr ull mask31 = (1ull<<31)-1ull;
        constexpr ull mask30 = (1ull<<30)-1ull;
        const ull au   = a>>31;
        const ull bu   = b>>31;
        const ull ad   = a&mask31;
        const ull bd   = b&mask31;
        const ull mid  = au*bd + bu*ad;
        const ull midu = mid>>30;
        const ull midd = mid&mask30;
        return mod(au*bu*2ull + midu+(midd<<31) + ad*bd);
    }
    ull root_pow(ull ex) {
        ull res = 1ull;
        ull bin = p_root;
        while (ex) {
            if (ex&1ull) res = mul(res, bin);
            bin = mul(bin, bin);
            ex >>= 1;
        }
        return res;
    }
    void set_base() {
        random_device rd;
        mt19937_64 rand(rd());
        for (ull &e : base) {
            while (!e) {
                const ull ex = rand();
                if (gcd(ex, p-1ull) != 1ull) continue;
                const ull b = root_pow(ex);
                if (b <= max_a+1ull) continue;
                if (find(base, base+base_n, b) != base+base_n) continue;
                e = b;
            }
        }
    }
    void set_base_pow(const int n) {
        const int prev_size = base_pow[0].size();
        for (int i = 0; i < base_n; ++i) {
            base_pow[i].resize(n);
            base_pow[i][0] = 1ull;
            for (int j = prev_size+1; j < n; ++j) {
                base_pow[i][j] = mul(base_pow[i][j-1], base[i]);
            }
        }
    }

  public:
    template<typename T>
    rolling_hash(T arr) {
        if (!base[0]) set_base();
        const int n = arr.size();
        if (base_pow[0].size() < (size_t)n) set_base_pow(n+1);
        for (int i = 0; i < base_n; ++i) {
            hash[i].resize(n+1);
            hash[i][0] = 0ull;
            for (int j = 0; j < n; ++j) {
                hash[i][j+1] = mod(mul(hash[i][j],base[i])+(ull)arr[j]+1ull);
            }
        }
    }
    vector<ull> subseq(int l, int r) { // 0-indexed, hankai-kukan
        vector<ull> res;
        for (int i = 0; i < base_n; ++i)  {
            ull h = hash[i][r] + p-mul(hash[i][l],base_pow[i][r-l]);
            if (h >= p) h -= p;
            res.push_back(h);
        }
        return res;
    }
};
uint64_t rolling_hash::base[];
vector<uint64_t> rolling_hash::base_pow[];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    int m;
    cin >> s >> m;
    int n = s.size();
    rolling_hash sh(s);
    map<vec<uint64_t>,int> p;
    for (int i = 1; i <= 10; ++i) {
        for (int j = 0; j+i <= n; ++j) {
            ++p[sh.subseq(j,j+i)];
        }
    }
    int c = 0;
    while (m--) {
        string t;
        cin >> t;
        c += p[rolling_hash(t).subseq(0,t.size())];
    }
    cout << c << '\n';
}
0