結果

問題 No.430 文字列検索
ユーザー みずくらげみずくらげ
提出日時 2019-04-21 11:03:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 296 ms / 2,000 ms
コード長 1,281 bytes
コンパイル時間 1,128 ms
コンパイル使用メモリ 89,284 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-04-15 10:34:56
合計ジャッジ時間 5,012 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 296 ms
8,192 KB
testcase_02 AC 292 ms
8,192 KB
testcase_03 AC 293 ms
8,192 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 8 ms
8,064 KB
testcase_09 AC 2 ms
6,948 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 293 ms
8,320 KB
testcase_12 AC 294 ms
8,320 KB
testcase_13 AC 293 ms
8,192 KB
testcase_14 AC 293 ms
8,320 KB
testcase_15 AC 292 ms
8,192 KB
testcase_16 AC 292 ms
8,320 KB
testcase_17 AC 293 ms
8,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
using namespace std;

typedef unsigned long long ull;
const ull B = 100000007;

int main() {
    string s;
    cin >> s;
    int m;
    cin >> m;
    vector<string> c(m);
    for (int i = 0; i < m; i++) {
        cin >> c[i];
    }

    vector<vector<ull> > hash_s;
    hash_s = vector<vector<ull> >(11, vector<ull>(s.size(), 0));

    for (int l = 1; l <= 10; l++) {
        if (l > s.size()) {
            break;
        }

        ull b_pow_l = 1;
        for (int i = 0; i < l; i++) {
             b_pow_l *= B;
        }

        ull hs = 0;
        for (int i = 0; i < l; i++) {
            hs = hs * B + s[i];
        }
        hash_s[l][0] = hs;

        for (int i = 0; i + l < s.size(); i++) {
            hs = hs * B + s[i + l] - s[i] * b_pow_l;
            hash_s[l][i+1] = hs;
        }
    }


    long long ans = 0;
    for (int i = 0; i < m; i++) {
        ull hc = 0;
        for (int j = 0; j < c[i].size(); j++) {
            hc = hc * B + c[i][j];
        }

        int l = c[i].size();
        for (int j = 0; j + l <= s.size(); j++) {
            if (hash_s[l][j] == hc) {
                ans++;
            }
        }
    }
    cout << ans << endl;

    return 0;
}
0