結果

問題 No.430 文字列検索
ユーザー cormorancormoran
提出日時 2016-10-18 17:28:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,677 bytes
コンパイル時間 1,575 ms
コンパイル使用メモリ 168,308 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-02 02:34:45
合計ジャッジ時間 2,532 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 24 ms
5,248 KB
testcase_02 AC 13 ms
5,376 KB
testcase_03 AC 12 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 17 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 23 ms
5,376 KB
testcase_12 AC 22 ms
5,376 KB
testcase_13 AC 22 ms
5,376 KB
testcase_14 AC 21 ms
5,376 KB
testcase_15 AC 19 ms
5,376 KB
testcase_16 AC 12 ms
5,376 KB
testcase_17 AC 13 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)

// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }

typedef unsigned long long ull;
const ull HashBase = 100000007;
vector<ull> pow_base;

vector<ull> build_rolling_hash(const string &s){
    int n = s.size();
    vector<ull> hash(n+1);
    pow_base.resize(n+1);
    hash[0] = 0;
    pow_base[0] = 1;
    rep(i, n){
        hash[i+1] = hash[i] * HashBase + s[i];
        pow_base[i+1] = pow_base[i] * HashBase;
    }
    return hash;
}

ull build_hash(const string &s) {
    ull base = 1;
    ull ret = 0;
    for(int i = s.size() - 1; i >= 0; i--) {
        ret += s[i] * base;
        base *= HashBase;
    }
    return ret;
}

// hash of (l,r] ( 1-based index)
ull get_rolling_hash(const vector<ull> &hash,int l,int r){
    return hash[r] - hash[l] * pow_base[r-l];
}

class Solver {
  public:
    bool solve() {
        string s; cin >> s;
        int m; cin >> m;
        unordered_set<ull> hash;
        rep(i, m) {
            string c; cin >> c;
            hash.insert(build_hash(c));
        }

        vector<ull> rh = build_rolling_hash(s);        
        ll ans = 0;
        repeat(i, 1, s.size() + 1) {
            rep(j, 10) {
                ans += hash.count(get_rolling_hash(rh, i - 1, i + j));
            }            
        }
        
        cout << ans << endl;
        
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}
0