結果

問題 No.430 文字列検索
ユーザー boutarou
提出日時 2021-01-04 16:26:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 258 ms / 2,000 ms
コード長 1,159 bytes
コンパイル時間 828 ms
コンパイル使用メモリ 85,828 KB
最終ジャッジ日時 2025-01-17 09:37:34
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
using namespace std;
using ll = long long;

struct RollingHash {
    ll base = 1007, MOD = 1e9 + 7;
    vector<ll> hash, pow;

    RollingHash(string s) {
        int n = (int)s.size();
        hash.assign(n + 1, 0);
        pow.assign(n + 1, 1);
        for (int i = 0; i < n; i++) {
            hash[i + 1] = (hash[i] * base + s[i]) % MOD;
            pow[i + 1] = pow[i] * base % MOD;
        }
    }

    ll get(int l, int r) {
        ll res = (hash[r] - hash[l] * pow[r - l]) % MOD;
        if (res < 0)
            res += MOD;
        return res;
    }
};

int main() {
    string s; cin >> s;
    int m; cin >> m;
    vector<string> c(m);
    for (int i = 0; i < m; i++) cin >> c[i];
    RollingHash rh(s);
    map<ll, ll> mp;
    for (int i = 1; i <= 10; i++) {
        for (int j = 0; j + i <= (int)s.size(); j++) {
            ll hash = rh.get(j, j + i);
            mp[hash]++;
        }
    }
    ll ans = 0;
    for (int i = 0; i < m; i++) {
        RollingHash now(c[i]);
        ll hash = now.get(0, (int)c[i].size());
        ans += mp[hash];
    }
    cout << ans << endl;
    return 0;
}
0