結果

問題 No.430 文字列検索
ユーザー takuwwwotakuwwwo
提出日時 2020-03-20 02:16:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,100 bytes
コンパイル時間 981 ms
コンパイル使用メモリ 109,264 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-08 03:40:47
合計ジャッジ時間 15,579 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1,066 ms
6,940 KB
testcase_02 AC 752 ms
6,940 KB
testcase_03 AC 583 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 7 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 4 ms
6,944 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 1,935 ms
6,940 KB
testcase_15 AC 1,602 ms
6,944 KB
testcase_16 AC 779 ms
6,940 KB
testcase_17 AC 693 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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