結果

問題 No.430 文字列検索
ユーザー takuwwwo
提出日時 2020-03-20 02:16:48
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,100 bytes
コンパイル時間 920 ms
コンパイル使用メモリ 108,128 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-10 00:42:05
合計ジャッジ時間 15,410 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <stack>
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <cfloat>
#include <math.h>
#include <numeric>
#include <string.h>
#include <sys/time.h>
#include <random>


#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;

template<typename T>
class KMP{
    vector<int> a;
    T s;

public:
    KMP(T s_arg): s(s_arg){
        int n = s_arg.size();
        a = vector<int>(n+1, -1);   // a[j+1]が[0, j]のboarderの最大の長さ
    }

    void constructBorderArray(){
        int j = -1;
        for(int i = 0; i < s.size(); i++){
            while(j >= 0 && s[i] != s[j]){
                j = a[j];
            }

            j++;
            if(s[i+1] == s[j]) a[i+1] = a[j];
            else a[i+1] = j;
        }
    }

    /**
     *
     * @param x: sとマッチングさせる配列
     * @return : マッチング位置
     */
    int search(T x, int pos=0){
        int j = 0;
        for(int i = pos; i < x.size(); i++){
            while(j >= 0 && x[i] != s[j]){
                j = a[j];
            }
            j++;
            if(j == s.size()){
                return i - j + 1;
            }
        }
        return -1;
    }

    int count(T x, int pos=0){
        int j = 0;
        int res = 0;
        for(int i = pos; i < x.size(); i++){
            while(j >= 0 && x[i] != s[j]){
                j = a[j];
            }
            j++;
            if(j == s.size()){
                res++;
                j = a[j];
            }
        }
        return res;
    }
};



int main(){
    string s;   cin >> s;
    int m;  cin >> m;

    int res = 0;
    for(int i = 0; i < m; i++){
        string t;   cin >> t;
        KMP<string> kmp(t);
        kmp.constructBorderArray();

        res += kmp.count(s);
    }

    cout << res << endl;


    return 0;
}
0