結果

問題 No.430 文字列検索
ユーザー goodbatongoodbaton
提出日時 2019-02-20 23:14:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,875 bytes
コンパイル時間 869 ms
コンパイル使用メモリ 97,028 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 11:45:47
合計ジャッジ時間 17,269 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1,119 ms
5,376 KB
testcase_02 AC 1,264 ms
5,376 KB
testcase_03 AC 722 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 6 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 1,996 ms
5,376 KB
testcase_15 AC 1,814 ms
5,376 KB
testcase_16 AC 1,277 ms
5,376 KB
testcase_17 AC 1,258 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
    out << "{" << p.first << ", " << p.second << "}";
    return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* KMP */

// A[i] := 文字列S[0,i-1]の接頭辞と接尾辞が最大何文字一致しているか (i-1未満)
// len(A) == S.size() + 1

void KMP(const char *S, int *A){
  A[0] = -1;
  int j = -1, n = strlen(S);
  for (int i = 0; i < n; i++) {
    while (j >= 0 && S[i] != S[j]) j = A[j];
    j++;

    //KMP
    //if (S[i+1] == S[j]) A[i+1] = A[j];
    //else A[i+1] = j;

    //MP
    A[i+1] = j;
  }
}

char s[SIZE];

int main(){
  int n, m, ans = 0;

  scanf("%s%d", s, &m);
  n = strlen(s);

  for(int i=0; i<m; i++) {
    char p[12] = {};
    int k[14];
    scanf("%s", p);
    p[strlen(p)] = '#';

    KMP(p, k);

    int t = 0;
    for(int j=0; j<n; j++) {
      if (p[t] == s[j]) {
        t++;
      } else {
        ans += p[t] == '#';
        j -= k[t] >= 0;
        t = max(0, k[t]);
      }
    }
    ans += p[t] == '#';
  }

  printf("%d\n", ans);

  return 0;
}

0