結果

問題 No.430 文字列検索
ユーザー fine
提出日時 2016-10-02 23:52:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 507 ms / 2,000 ms
コード長 913 bytes
コンパイル時間 1,578 ms
コンパイル使用メモリ 169,060 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-10 00:05:23
合計ジャッジ時間 7,390 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;

const ull B = 100000007;

//bの中にaは何個ある?
int countSubString(string a, string b) {
    int al = a.length(), bl = b.length();
    if (al > bl) return 0;
    
    ull t = 1;
    for (int i = 0; i < al; i++) t *= B;
    
    ull 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];
    
    int ret = 0;
    for (int i = 0; i + al <= bl; i++) {
        if (ah == bh) ret++;
        if (i + al < bl) bh = bh * B + b[i + al] - t * b[i];
    }
    
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string b;
    cin >> b;
    int m;
    cin >> m;
    int ans = 0;
    for (int i = 0; i < m; i++) {
        string a;
        cin >> a;
        ans += countSubString(a, b);
    }
    cout << ans << "\n";
    return 0;
}
0