結果

問題 No.430 文字列検索
ユーザー tatsukawatatsukawa
提出日時 2016-10-28 19:33:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 577 ms / 2,000 ms
コード長 813 bytes
コンパイル時間 1,378 ms
コンパイル使用メモリ 145,524 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 20:47:36
合計ジャッジ時間 8,108 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = unsigned long long;
using P = std::pair<int, int>;

const int INF = 1e8;

const ll B = 100000007;

int cnt(string b, string a) {
   
    int res = 0; 
    int al = a.size();
    int bl = b.size();

    ll t = 1;
    for(int i = 0; i < al; i++) t *= B;

    ll ah = 0, bh = 0;
    for(int i = 0; i < al; i++) ah = ah * B + a[i];
    for(int i = 0; i < al; i++) bh = bh * B + b[i];

    for(int i = 0; i + al <= bl; i++) {
        if(ah == bh) res++;
        if(i + al < bl) bh = bh * B + b[i+al] - b[i] * t;
    }

    return res;
}

int main() {
    ios::sync_with_stdio(false);

    string S, C;
    int M;
    int ans = 0;
    cin >> S >> M;

    for(int i = 0; i < M; i++) {
        cin >> C;
        ans += cnt(S, C);
    }
    cout << ans << endl;
}
0