結果

問題 No.430 文字列検索
ユーザー tonyu0
提出日時 2021-01-04 15:59:25
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,924 ms / 2,000 ms
コード長 1,396 bytes
コンパイル時間 4,082 ms
コンパイル使用メモリ 157,508 KB
最終ジャッジ日時 2025-01-17 09:37:11
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

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() + 1);
  // 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