結果

問題 No.430 文字列検索
ユーザー raven7959raven7959
提出日時 2022-01-02 14:02:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,699 ms / 2,000 ms
コード長 3,057 bytes
コンパイル時間 2,285 ms
コンパイル使用メモリ 198,688 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 20:15:12
合計ジャッジ時間 13,709 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 564 ms
5,376 KB
testcase_02 AC 705 ms
5,376 KB
testcase_03 AC 425 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 1,684 ms
5,376 KB
testcase_12 AC 1,678 ms
5,376 KB
testcase_13 AC 1,699 ms
5,376 KB
testcase_14 AC 1,677 ms
5,376 KB
testcase_15 AC 1,337 ms
5,376 KB
testcase_16 AC 711 ms
5,376 KB
testcase_17 AC 691 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define yn(joken) cout<<((joken) ? "Yes" : "No")<<endl
#define YN(joken) cout<<((joken) ? "YES" : "NO")<<endl
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        os << v[i];
        if (i < int(v.size()) - 1) os << ' ';
    }
    return os;
}

template <class T>
struct KnuthMorrisPrattAlgorithm{
    string pattern;
    int len;
    vector<int> kmpNext;
    KnuthMorrisPrattAlgorithm(T P): pattern(P+'$'), len(int(P.size())+1), kmpNext(int(P.size())+2){}

    void build(){
        kmpNext[0]=-1;
        int j=-1;
        for(int i=0;i<len;i++){
            while(j>=0 && pattern[i]!=pattern[j]) j=kmpNext[j];
            j++;
            if(pattern[i+1]==pattern[j]) kmpNext[i+1]=kmpNext[j];
            else kmpNext[i+1]=j;
        }
    }

    vector<int> kmpSearch(T TEXT) {
        string text=TEXT+'%';
        int text_pos=0; // テキストの比較位置
        int pattern_pos=0; // パターンの比較位置
        vector<int> ret; // 見つかったパターンの先頭の位置を全て返す(0-indexed)

        build();
        while (text_pos < int(text.size())) {
            if (text[text_pos] == pattern[pattern_pos]) {
                    text_pos++;
                    pattern_pos++;
            } 
            else {
                if(pattern_pos==len-1) ret.push_back(text_pos-pattern_pos);
                if (kmpNext[pattern_pos] == kmpNext[0]) {
                    text_pos++;
                    pattern_pos = 0;
                }
                else pattern_pos = kmpNext[pattern_pos];
            }
        }
        return ret;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    string S;
    cin>>S;
    int M;
    cin>>M;
    ll ans=0;
    rep(_,M){
        string C;
        cin>>C;
        KnuthMorrisPrattAlgorithm<string> KMP(C);
        ans+=sz(KMP.kmpSearch(S));
    }
    cout<<ans<<endl;
}
0