結果

問題 No.430 文字列検索
ユーザー tonyu0tonyu0
提出日時 2021-01-04 15:59:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,854 ms / 2,000 ms
コード長 1,396 bytes
コンパイル時間 1,497 ms
コンパイル使用メモリ 127,412 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 02:16:23
合計ジャッジ時間 14,686 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 823 ms
5,248 KB
testcase_02 AC 792 ms
5,376 KB
testcase_03 AC 474 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 5 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 1,854 ms
5,376 KB
testcase_12 AC 1,837 ms
5,376 KB
testcase_13 AC 1,854 ms
5,376 KB
testcase_14 AC 1,730 ms
5,376 KB
testcase_15 AC 1,505 ms
5,376 KB
testcase_16 AC 846 ms
5,376 KB
testcase_17 AC 788 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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