結果

問題 No.430 文字列検索
ユーザー tonyu0tonyu0
提出日時 2021-01-04 16:04:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,977 ms / 2,000 ms
コード長 1,301 bytes
コンパイル時間 1,012 ms
コンパイル使用メモリ 115,104 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-27 03:33:29
合計ジャッジ時間 14,890 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#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)

int ans;
template <typename T>
void kmpSearch(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);
      ++ans;
      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;
  for (int i = 0; i < m; ++i) {
    string tc;
    cin >> tc;
    vector<uint8_t> c(tc.begin(), tc.end());
    kmpSearch(s, c);
  }
  cout << ans << '\n';
  return 0;
}
0