結果

問題 No.430 文字列検索
ユーザー Ogtsn99Ogtsn99
提出日時 2019-09-17 20:39:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,818 ms / 2,000 ms
コード長 1,564 bytes
コンパイル時間 1,639 ms
コンパイル使用メモリ 171,072 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 20:15:18
合計ジャッジ時間 14,684 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 837 ms
4,376 KB
testcase_02 AC 605 ms
4,376 KB
testcase_03 AC 599 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 1,818 ms
4,380 KB
testcase_12 AC 1,814 ms
4,376 KB
testcase_13 AC 1,805 ms
4,380 KB
testcase_14 AC 1,734 ms
4,376 KB
testcase_15 AC 1,457 ms
4,376 KB
testcase_16 AC 691 ms
4,376 KB
testcase_17 AC 605 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rrep(i,n) for(int (i)=((n)-1);(i)>=0;(i)--)
#define itn int
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
const long long INF = 1LL << 60;
const int MOD = 1000000007;
struct KMP{
    vector <int> table;
    string p;
    int sz;
    KMP(string S){ //探したいword, pattern
        sz = S.size();
        p = S;
        table.resize(sz+1);
        int j = table[0] = -1;
        for (int i = 0; i < S.size(); i++) {
            while (j >= 0 && S[i] != S[j]) j = table[j];
            j++;
            //table[i+1] = j;
            if (S[i+1] == S[j]) table[i+1] = table[j]; 
            else table[i+1] = j;
        }
        //rep(i, table.size()) cout<<table[i]<<endl;
    }
    /*vector <pair<int,int>>*/
    int findfrom(string t) { //text
        int n = t.size();
        int count = 0;
        vector <pair<int,int>> data;
        for (int i = 0, k = 0; i < n; ++i) {
            while (k >= 0 && p[k] != t[i]) k = table[k];
            if (++k >= sz) {
                //data.push_back({i-sz+1,i});
                k = table[k];
                count++;
            }
        }
        return count;
        //return data; //0indexed 
    }
};
signed main(void){
    string text; cin>>text;
    int n; cin>>n;
    int ans = 0;
    rep(i,n){
        string tmp; cin>>tmp;
        KMP kmp(tmp);
        int cnt = kmp.findfrom(text);
        //cout<<cnt<<endl;
        ans += cnt;
    }
    cout<<ans<<endl;
}
0