結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-10-18 17:21:33 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 31 ms / 2,000 ms | 
| コード長 | 1,312 bytes | 
| コンパイル時間 | 627 ms | 
| コンパイル使用メモリ | 79,744 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:10:40 | 
| 合計ジャッジ時間 | 1,294 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
#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;
}
            
            
            
        