結果

問題 No.430 文字列検索
ユーザー yuto1115yuto1115
提出日時 2020-06-10 00:14:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,789 ms / 2,000 ms
コード長 3,761 bytes
コンパイル時間 2,230 ms
コンパイル使用メモリ 206,288 KB
実行使用メモリ 5,140 KB
最終ジャッジ日時 2023-09-01 23:17:55
合計ジャッジ時間 21,458 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1,770 ms
4,920 KB
testcase_02 AC 1,779 ms
5,140 KB
testcase_03 AC 1,780 ms
4,916 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 11 ms
4,900 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 1,789 ms
4,940 KB
testcase_12 AC 1,780 ms
4,976 KB
testcase_13 AC 1,787 ms
4,972 KB
testcase_14 AC 1,784 ms
4,916 KB
testcase_15 AC 1,784 ms
4,916 KB
testcase_16 AC 1,776 ms
4,908 KB
testcase_17 AC 1,770 ms
4,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = (s); i < (n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==v.size()-1?']':','); return os; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

using ull = unsigned long long;
const ull mod = (1ul<<61)-1;
const ull mask30 = (1ul<<30)-1;
const ull mask31 = (1ul<<31)-1;
const ull mask61 = mod;

ull calc_mod(ull x) {
    ull xu = x>>61;
    ull xd = x&mask61;
    ull res = xu+xd;
    if(res >= mod) res -= mod;
    return res;
}

// a*b mod 2^61-1
ull mul(ull a,ull b) {
    ull au = a>>31;
    ull ad = a&mask31;
    ull bu = b>>31;
    ull bd = b&mask31;
    ull mid = ad*bu+au*bd;
    ull midu = mid>>30;
    ull midd = mid&mask30;
    return au*bu*2+midu+(midd<<31)+ad*bd;
}

class rolling_hash {
    ull base1;
    ull base2;
    int n;
    string s;
    vector<ull> hash1,hash2,pow1,pow2;

    void init() {
        random_device rnd;
        mt19937_64 mt(rnd());
        base1 = calc_mod(mt());
        base2 = calc_mod(mt());
        while(base1 < 2 || base1 > mod-2) base1 = calc_mod(mt());
        while(base2 < 2 || base2 > mod-2) base2 = calc_mod(mt());
        hash1.assign(n+1,0);
        hash2.assign(n+1,0);
        pow1.assign(n+1, 1);
        pow2.assign(n+1,1);
        rep(i,n) {
            hash1[i+1] = calc_mod(mul(hash1[i],base1)+s[i]);
            hash2[i+1] = calc_mod(mul(hash2[i],base2)+s[i]);
            pow1[i+1] = calc_mod(mul(pow1[i], base1));
            pow2[i+1] = calc_mod(mul(pow2[i], base2));
        }
    }

public:
    rolling_hash(string s):s(s),n(s.size()) {
        init();
    }
    // return hash of [l,r)
    pair<ull,ull> get(int l,int r) {
        ll res1 = calc_mod(hash1[r]+mod*4-mul(hash1[l], pow1[r-l]));
        ll res2 = calc_mod(hash2[r]+mod*4-mul(hash2[l], pow2[r-l]));
        return make_pair(res1,res2);
    }
    int count(string t) {
        if(t.size() > n) return 0;
        ull ht1 = 0,ht2 = 0;
        rep(i,t.size()) {
            ht1 = calc_mod(mul(ht1,base1)+t[i]);
            ht2 = calc_mod(mul(ht2,base2)+t[i]);
        }
        pair<ull,ull> ht = make_pair(ht1,ht2);
        int res = 0;
        rep(i,n-t.size()+1) {
            if(get(i,i+t.size()) == ht) res++;
        }
        return res;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    string s;
    int m;
    cin >> s >> m;
    int ans = 0;
    rolling_hash rh(s);
    rep(i,m) {
        string c;
        cin >> c;
        ans += rh.count(c);
    }
    cout << ans << endl;
}
0