結果

問題 No.430 文字列検索
ユーザー もりをもりを
提出日時 2019-07-11 16:06:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 2,555 bytes
コンパイル時間 1,900 ms
コンパイル使用メモリ 173,960 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 10:29:47
合計ジャッジ時間 2,756 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int (i)=0;(i)<(n);++(i))
#define FOR(i,a,b) for(int (i)=(a);(i)<(b);++(i))
#define EACH(e,v) for(auto& e:v)
#define ALL(v) (v).begin(),(v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define PERM(v) SORT(v);for(bool c##p=1;c##p;c##p=next_permutation(ALL(v)))
#define UNIQUE(v) SORT(v);(v).erase(unique(ALL(v)),(v).end())
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
// #define int long

const int MOD = (int)1e9 + 7;
const int INF = 1 << 30;
const ll INFF = 1LL << 62;

// 下にy, 右にx
enum {R, U, L, D};
const int dx[] = {1,  0, -1, 0};
const int dy[] = {0, -1,  0, 1};

template<int mod, int base=10007>
struct RollingHash {
    vector<ll> hsh, pwr;
    ll umod(ll n) { return (n % mod + mod) % mod; }
    RollingHash() {}
    RollingHash(const string &s) {
        int sz = (int)s.size();
        hsh.assign(sz + 1, 0);
        pwr.assign(sz + 1, 0);  pwr[0] = 1;
        for (int i = 0; i < sz; ++i) {
            hsh[i + 1] = umod(hsh[i] * base + s[i]);
            pwr[i + 1] = umod(pwr[i] * base);
        }
    }
    // [l, r)
    ll get(int l, int r) {
        return umod(hsh[r] - hsh[l] * pwr[r - l]);
    }
    // h1 <- h2
    ll join(ll h1, ll h2, int h2_sz) {
        return umod(h1 * pwr[h2_sz] + h2);
    }
};
using RH1 = RollingHash<(int)1e9 + 7>;
using RH2 = RollingHash<(int)1e9 + 9>;
using RH = pair<RH1, RH2>;
using H = pair<ll, ll>;
RH init(const string &s) { return make_pair(RH1(s), RH2(s)); }
H get(RH &rh, int l, int r) { return make_pair(rh.first.get(l, r), rh.second.get(l, r)); }
// void get(RH &rh, int l, int r, ll &h1, ll &h2) {
//     h1 = rh.first.get(l, r);
//     h2 = rh.second.get(l, r);
// }

signed main() {

    string S;
    int M;

    cin >> S >> M;
    int sz = S.size();

    RH S_rh = init(S);

    set<H> C_rh[11];
    REP(i, M) {
        string c;
        cin >> c;
        int n = c.size();
        RH tmp = init(c);
        H tmp2 = get(tmp, 0, n);
        // cerr << tmp2.first << ' ' << tmp2.second << endl;
        C_rh[n].insert(tmp2);
        // C_rh[n].emplace_back(init(c));
    }

    ll res = 0;
    REP(i, 11) {
        for (int l = 0; l < sz; ++l) {
            int r = l + i;
            if (r > sz) break;
            if (C_rh[i].count(get(S_rh, l, r))) ++res;
        }
    }
    cout << res << endl;

}
0