結果

問題 No.430 文字列検索
ユーザー Rubikun
提出日時 2019-12-10 11:48:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,991 ms / 2,000 ms
コード長 1,386 bytes
コンパイル時間 1,662 ms
コンパイル使用メモリ 171,084 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-10 00:39:29
合計ジャッジ時間 16,000 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=1003,INF=1<<30;

int dp[MAX][53][53];

struct KMP{
    vector<int> A;
    string S;
    
    void init(string &T){
        S=T;
        A.assign(S.size()+1,0);
        A[0]=-1;
        int j=-1;
        
        for(int i=0;i<S.size();i++){
            while(j>=0&&S[i]!=S[j]) j=A[j];
            j++;
            
            A[i+1]=j;
        }
    }
    
    vector<int> match(string &C,string &T){
        init(C);
        vector<int> B;
        int m=0,i=0;
        
        while(m+i<T.size()){
            if(S[i]==T[m+i]){
                i++;
                if(i==S.size()){
                    B.push_back(m);
                    m+=i-A[i];
                    i=A[i];
                }
            }else{
                m+=i-A[i];
                if(i>0) i=A[i];
            }
        }
        
        return B;
    }//SがTの中に含まれる開始位置を返す
};

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    string S;cin>>S;
    int M;cin>>M;
    
    ll sum=0;
    
    while(M--){
        string T;cin>>T;
        KMP k;
        sum+=k.match(T,S).size();
        
        //cout<<sum<<endl;
    }
    
    cout<<sum<<endl;
    
}

0