結果

問題 No.430 文字列検索
ユーザー みずくらげみずくらげ
提出日時 2019-11-17 18:09:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,070 ms / 2,000 ms
コード長 2,178 bytes
コンパイル時間 1,650 ms
コンパイル使用メモリ 106,224 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-25 21:20:38
合計ジャッジ時間 12,511 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1,054 ms
4,348 KB
testcase_02 AC 1,052 ms
4,348 KB
testcase_03 AC 1,051 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 8 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 5 ms
4,348 KB
testcase_11 AC 1,049 ms
4,348 KB
testcase_12 AC 1,070 ms
4,348 KB
testcase_13 AC 1,048 ms
4,348 KB
testcase_14 AC 1,053 ms
4,348 KB
testcase_15 AC 1,061 ms
4,348 KB
testcase_16 AC 1,055 ms
4,348 KB
testcase_17 AC 1,054 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iomanip>
#include <cassert>
#include <random>
#include <tuple>
#include <cstdint>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int INF = INT32_MAX;


struct RollingHash {
    const uint64_t MASK30 = (1uLL << 30) - 1;
    const uint64_t MASK31 = (1uLL << 31) - 1;
    const uint64_t MOD = (1uLL << 61) - 1;
    const uint64_t POSITIVIZER = MOD * ((1uLL << 3) - 1);
    static uint32_t Base;
    vector<uint64_t> powMemo;
    vector<uint64_t> hash;

    RollingHash(const string &s) {
        int n = (int)s.size();
        hash.assign(n+1, 0);
        powMemo.assign(n+1, 1);

        for (int i = 0; i < n; i++) {
            hash[i+1] = CalcMod(Mul(hash[i], Base) + s[i]);
            powMemo[i+1] = CalcMod(Mul(powMemo[i], Base));
        }
    }

    inline uint64_t& operator[] (size_t idx) {
        return hash[idx];
    }

    inline uint64_t Slice(int start, int len) {
        return CalcMod(hash[start + len] + POSITIVIZER - Mul(hash[start], powMemo[len]));
    }

    inline uint64_t CalcMod(uint64_t val) {
        val = (val & MOD) + (val >> 61);
        if (val > MOD) val -= MOD;
        return val;
    }

    inline uint64_t Mul(uint64_t a, uint64_t b) {
        uint64_t au = a >> 31;
        uint64_t ad = a & MASK31;
        uint64_t bu = b >> 31;
        uint64_t bd = b & MASK31;
        uint64_t mid = ad * bu + au * bd;
        uint64_t midu = mid >> 30;
        uint64_t midd = mid & MASK30;
        return ((au * bu) << 1) + midu + (midd << 31) + ad * bd;
    }
};
uint32_t RollingHash::Base = rand() + 1000;


int main() {
    string s; cin >> s;
    int m; cin >> m;

    RollingHash rh(s);

    int ans = 0;
    
    rep(i, m) {
        string c; cin >> c;
        RollingHash rh_c(c);
        for (int j = 0; j < (int)s.size() - (int)c.size() + 1; j++) {
            if (rh.Slice(j, c.size()) == rh_c[c.size()]) ans++;
        }
    }
    
    cout << ans << endl;

    return 0;
}
0