結果

問題 No.430 文字列検索
ユーザー alorie10alorie10
提出日時 2020-11-17 21:12:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,998 ms / 2,000 ms
コード長 855 bytes
コンパイル時間 2,514 ms
コンパイル使用メモリ 202,256 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-30 14:25:55
合計ジャッジ時間 17,597 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 560 ms
4,376 KB
testcase_02 AC 976 ms
4,376 KB
testcase_03 AC 594 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 1,995 ms
4,380 KB
testcase_12 AC 1,998 ms
4,380 KB
testcase_13 AC 1,992 ms
4,376 KB
testcase_14 AC 1,990 ms
4,376 KB
testcase_15 AC 1,696 ms
4,380 KB
testcase_16 AC 1,040 ms
4,380 KB
testcase_17 AC 987 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
vector<int> table,res;
int n;
string s,t;
vector<int> pre_kmp(string s){
    vector<int> v(s.size()+1);
    int k;
    v[0]=-1;
    for(int i=1;i<=s.size();i++){
        k=v[i-1];
        while(k>=0){
            if(s[k]==s[i-1])break;
            else k=v[k];
        }
        v[i]=k+1;
    }
    return v;
}
void match(string s, string t){
    int head = 0, j = 0, slen=s.size(),tlen=t.size();
    while(head + j < slen){
        if(t[j] == s[head + j]){
            if(++j != tlen) continue;
            res.push_back(head);
        }
        head += j - table[j], j = max(table[j], 0);
    }
}
int main(void){
    cin>>s>>n;
    for(int i=0;i<n;i++){
        cin>>t;
        table=pre_kmp(t);
        match(s,t);
    }
    //for(auto x:res)cout<<x<<endl;
    cout<<res.size()<<endl;
}
0