結果

問題 No.430 文字列検索
ユーザー OUDONOUDON
提出日時 2016-11-18 02:27:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 505 ms / 2,000 ms
コード長 1,261 bytes
コンパイル時間 1,595 ms
コンパイル使用メモリ 166,388 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-17 08:27:37
合計ジャッジ時間 7,401 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 505 ms
4,376 KB
testcase_02 AC 505 ms
4,380 KB
testcase_03 AC 504 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 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 505 ms
4,380 KB
testcase_12 AC 504 ms
4,376 KB
testcase_13 AC 504 ms
4,380 KB
testcase_14 AC 504 ms
4,380 KB
testcase_15 AC 503 ms
4,380 KB
testcase_16 AC 505 ms
4,380 KB
testcase_17 AC 505 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
#define BINARY(x) static_cast<bitset<16> >(x)

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()

typedef long long ll;
typedef unsigned long long ull;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};

const ull B = 100000007;

int count(string a, string b)
{
    int al = a.size(), bl = b.size();
    if (bl > al) return 0;

    int res = 0;
    ull t = 1;
    for (int i=0; i<bl; i++) t *= B;

    ull ah = 0, bh = 0;
    rep(i, bl) ah = ah * B + a[i];
    rep(i, bl) bh = bh * B + b[i];

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

    return res;
}

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

    string S;
    int M;
    cin >> S >> M;

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