結果
問題 | No.430 文字列検索 |
ユーザー | zkou |
提出日時 | 2020-11-13 16:58:32 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,233 bytes |
コンパイル時間 | 1,922 ms |
コンパイル使用メモリ | 198,560 KB |
実行使用メモリ | 4,500 KB |
最終ジャッジ日時 | 2023-09-30 02:13:01 |
合計ジャッジ時間 | 9,668 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
4,380 KB |
testcase_01 | AC | 538 ms
4,380 KB |
testcase_02 | AC | 846 ms
4,380 KB |
testcase_03 | AC | 559 ms
4,380 KB |
testcase_04 | AC | 1 ms
4,376 KB |
testcase_05 | AC | 2 ms
4,376 KB |
testcase_06 | AC | 2 ms
4,384 KB |
testcase_07 | AC | 2 ms
4,376 KB |
testcase_08 | AC | 4 ms
4,376 KB |
testcase_09 | AC | 2 ms
4,380 KB |
testcase_10 | AC | 3 ms
4,376 KB |
testcase_11 | AC | 1,943 ms
4,380 KB |
testcase_12 | AC | 1,943 ms
4,376 KB |
testcase_13 | AC | 1,941 ms
4,380 KB |
testcase_14 | TLE | - |
testcase_15 | AC | 1,954 ms
4,376 KB |
testcase_16 | AC | 951 ms
4,376 KB |
testcase_17 | AC | 868 ms
4,500 KB |
ソースコード
#include <bits/stdc++.h> // #pragma GCC target("avx2") #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") using namespace std; int T[] = {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int kmp_count(string &s, string &w) { // テーブル構築 int i = 2, j = 0; while (i <= w.size()) { if (w[j] == w[i - 1]) { j++; T[i] = j; i++; } else if (j > 0) { j = T[j]; } else { T[i] = 0; i++; } } // 検索 int ret = 0; int m = 0; i = 0; while (m + i < s.size()) { if (w[i] == s[m + i]) { i++; if (i == w.size()) { ret++; m = m + i - T[i]; i = T[i]; } } else { m = m + i - T[i]; if (i > 0) { i = T[i]; } } } // cout << ret << endl; return ret; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); string S; cin >> S; int M; cin >> M; string C; int ans = 0; while (M--) { cin >> C; ans += kmp_count(S, C); } cout << ans << '\n'; return 0; }