結果

問題 No.430 文字列検索
ユーザー kimiyukikimiyuki
提出日時 2016-10-18 17:21:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,312 bytes
コンパイル時間 994 ms
コンパイル使用メモリ 79,572 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-03 04:42:10
合計ジャッジ時間 1,625 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 31 ms
5,376 KB
testcase_02 AC 7 ms
5,376 KB
testcase_03 AC 8 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 5 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 15 ms
5,376 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 10 ms
5,376 KB
testcase_16 AC 6 ms
5,376 KB
testcase_17 AC 7 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include <utility>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
using namespace std;
int main() {
    // prepare
    const int limit = 10;
    const int p = 1e9+7;
    const int q = 1e9+9;
    uint64_t pow_p[limit]; pow_p[0] = 1; repeat (i, limit-1) pow_p[i+1] = pow_p[i] * p;
    uint64_t pow_q[limit]; pow_q[0] = 1; repeat (i, limit-1) pow_q[i+1] = pow_q[i] * q;
    // input
    string s; int n; cin >> s >> n;
    vector<string> cs(n); repeat (i,n) cin >> cs[i];
    // compute
    int ans = 0;
    repeat_from (len, 1, limit+1) {
        set<pair<uint64_t,uint64_t> > hashes;
        for (string c : cs) if (c.length() == len) {
            uint64_t hp = 0, hq = 0;
            for (char a : c) {
                hp = hp * p + a;
                hq = hq * q + a;
            }
            hashes.emplace(hp, hq);
        }
        uint64_t hp = 0, hq = 0;
        repeat (i, (int)s.length()) {
            char c = i - len >= 0 ? s[i - len] : 0;
            hp = (hp - c * pow_p[len-1]) * p + s[i];
            hq = (hq - c * pow_q[len-1]) * q + s[i];
            if (hashes.count(make_pair(hp, hq))) ans += 1;
        }
    }
    // output
    cout << ans << endl;
    return 0;
}
0