結果

問題 No.430 文字列検索
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-10-18 17:18:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,577 bytes
コンパイル時間 1,631 ms
コンパイル使用メモリ 172,068 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 17:51:29
合計ジャッジ時間 15,749 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        auto i = vs.begin();
        os << "[" << *i;
        for (++i; i != vs.end(); ++i) os << " " << *i;
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    struct KMP {
        string s;
        vector<int> PMA;
        KMP(const string &s) : s(s) {
            buildPMA(s);
        }
        /* t縺ォ蜷ォ縺セ繧後ks縺ョ謨ー繧呈焚縺医k.
         * 縺薙・螳溯」・〒縺ッ
         *   s = "ABA",
         *   t = "ABABA"
         * 縺ョ縺ィ縺・ ( [0, 3), [2, 5)縺ァ繝槭ャ繝・) 繧定ソ斐☆.
         * [2, 5)縺ァ繝槭ャ繝√&縺帙◆縺上↑縺代l縺ーbuildPMA縺ァPMA[M]繧・縺ォ縺励※縺翫¥. */
        int match(const string& t) {
            int N = t.size(), M = s.size();
            int Count = 0;
            int si = 0, ti = 0;
            while (ti < N) {
                if (s[si] == t[ti]) {
                    si++; ti++;
                } else {
                    if (si == 0) ti++;
                    si = PMA[si];
                }
                if (si == M) {
                    Count++; // [ti - M, ti)縺ァ繝槭ャ繝・                    si = PMA[si];
                }
            }
            return Count;
        }
        /* Pattern Matching Automaton 繧剃ス懊k */
        void buildPMA(const string& s) {
            int M = s.size();
            PMA.resize(M + 1);
            PMA[0] = 0; PMA[1] = 0;
            for (int i = 2; i <= M; i++) {
                int j = PMA[i - 1];
                while (true) {
                    if (s[j] == s[i - 1]) { PMA[i] = j + 1; break; }
                    if (j == 0) { PMA[i] = 0; break; }
                    j = PMA[j];
                }
            }
        }
    };

    string S;
    int M;
    vector<string> C;
    void input() {
        cin >> S;
        cin >> M;
        C.clear(); C.resize(M);
        for (int i = 0; i < M; i++) cin >> C[i];
    }

    void solve() {
        int ans = 0;
        for (int i = 0; i < M; i++) {
            const string& c = C[i];
            KMP kmp(c);
            ans += kmp.match(S);
        }
        cout << ans << endl;
    }
}

int main() {
    input(); solve();
    return 0;
}

0