結果

問題 No.430 文字列検索
ユーザー tonyu0tonyu0
提出日時 2021-01-04 15:47:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,391 bytes
コンパイル時間 1,278 ms
コンパイル使用メモリ 127,748 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-23 02:06:05
合計ジャッジ時間 3,153 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 5 ms
6,940 KB
testcase_09 RE -
testcase_10 AC 4 ms
6,940 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (n); ++i)
#define rrep(i, j, n) for (int i = (n)-1; j <= i; --i)

template <typename T>
vector<int> search(const vector<T>& s, const vector<T>& pattern) {
  vector<int> table(pattern.size());
  // table[i] := when pattern[i] is not matched, next start from
  // pattern[table[i]]
  table[0] = -1;
  for (int i = 0, len = -1; i < (int)pattern.size(); ++i) {
    while (len >= 0 && pattern[i] != pattern[len]) len = table[len];
    ++len;
    table[i + 1] = len;
  }

  vector<int> pos;
  for (int i = 0, len = 0; i < (int)s.size(); ++i) {
    while (len >= 0 && s[i] != pattern[len]) len = table[len];
    ++len;
    if (len == pattern.size()) {
      pos.push_back(i - len + 1);
      len = table[len];
    }
  }
  return pos;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  string ts;
  cin >> ts;
  vector<uint8_t> s(ts.begin(), ts.end());
  int m;
  cin >> m;
  int ans = 0;
  for (int i = 0; i < m; ++i) {
    string tc;
    cin >> tc;
    vector<uint8_t> c(tc.begin(), tc.end());
    vector<int> res = search(s, c);
    ans += res.size();
  }
  cout << ans << '\n';
  return 0;
}
0