結果

問題 No.430 文字列検索
ユーザー RubikunRubikun
提出日時 2019-12-10 11:39:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,340 bytes
コンパイル時間 1,550 ms
コンパイル使用メモリ 170,188 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-06 05:18:29
合計ジャッジ時間 18,123 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 843 ms
4,376 KB
testcase_02 AC 1,000 ms
4,376 KB
testcase_03 AC 704 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 1,996 ms
4,380 KB
testcase_15 AC 1,735 ms
4,380 KB
testcase_16 AC 1,040 ms
4,380 KB
testcase_17 AC 994 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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 &T){
        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;
    }//Tの中にSが含まれる開始位置を返す
};

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;
        k.init(T);
        sum+=k.match(S).size();
    }
    
    cout<<sum<<endl;
    
}

0