結果

問題 No.430 文字列検索
ユーザー t33ft33f
提出日時 2020-04-13 00:48:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 987 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 71,560 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-23 22:33:16
合計ジャッジ時間 18,205 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1,192 ms
4,348 KB
testcase_02 AC 1,115 ms
4,348 KB
testcase_03 AC 817 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 7 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 5 ms
4,348 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 1,880 ms
4,348 KB
testcase_16 AC 1,190 ms
4,348 KB
testcase_17 AC 1,141 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <numeric>
#include <vector>
#include <iostream>
using namespace std;
vector<bool> kmp(string& pattern, string& text) {
  vector<int> kmp_next(pattern.size()+1);
  kmp_next[0] = -1;
  for (int i = 0, j = -1; i < pattern.size(); ) {
    while (j >= 0 && pattern[i] != pattern[j])
      j = kmp_next[j];
    i++; j++;
    if (i < pattern.size() && pattern[i] == pattern[j])
      kmp_next[i] = kmp_next[j];
    else
      kmp_next[i] = j;
  }
  vector<bool> matches(text.size(), false);
  for (int i = 0, j = 0; j < text.size(); ) {
    while (i >= 0 && pattern[i] != text[j])
      i = kmp_next[i];
    i++; j++;
    if (i == pattern.size()) {
      matches[j - i] = true;
      i = kmp_next[i];
    }
  }
  return matches;
};

int main() {
    string s; cin >> s;
    int m; cin >> m;
    int ans = 0;
    while (m--) {
      string t; cin >> t;
      vector<bool> matches = kmp(t, s);
      ans += accumulate(matches.begin(), matches.end(), 0);
    }
    cout << ans << endl;
}
0