結果

問題 No.430 文字列検索
ユーザー takuwwwotakuwwwo
提出日時 2020-03-20 02:16:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,899 ms / 2,000 ms
コード長 2,100 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 107,072 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 21:21:16
合計ジャッジ時間 14,786 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 848 ms
4,380 KB
testcase_02 AC 775 ms
4,380 KB
testcase_03 AC 603 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 1,898 ms
4,376 KB
testcase_12 AC 1,899 ms
4,380 KB
testcase_13 AC 1,891 ms
4,376 KB
testcase_14 AC 1,803 ms
4,376 KB
testcase_15 AC 1,521 ms
4,376 KB
testcase_16 AC 837 ms
4,384 KB
testcase_17 AC 780 ms
4,376 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