結果

問題 No.430 文字列検索
ユーザー pekempeypekempey
提出日時 2016-10-02 22:39:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,064 ms / 2,000 ms
コード長 845 bytes
コンパイル時間 1,389 ms
コンパイル使用メモリ 149,904 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 17:22:11
合計ジャッジ時間 12,889 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1,060 ms
4,380 KB
testcase_02 AC 1,063 ms
4,380 KB
testcase_03 AC 1,064 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 1,058 ms
4,380 KB
testcase_12 AC 1,062 ms
4,380 KB
testcase_13 AC 1,058 ms
4,380 KB
testcase_14 AC 1,057 ms
4,380 KB
testcase_15 AC 1,062 ms
4,380 KB
testcase_16 AC 1,058 ms
4,380 KB
testcase_17 AC 1,061 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct StringMatchingAutomaton {
	vector<int> kmp;
	vector<vector<int>> next;

	StringMatchingAutomaton(string s) : kmp(s.size() + 1), next(s.size() + 1, vector<int>(26)) {
		for (int i = 0; i < s.size(); i++) next[i][s[i] - 'A'] = i + 1;
		for (int i = 1; i <= s.size(); i++) {
			for (int j = 0; j < 26; j++) {
				if (i == s.size() || s[i] != j + 'A') {
					next[i][j] = next[kmp[i]][j];
				} else {
					kmp[i + 1] = next[kmp[i]][j];
				}
			}
		}
	}

	vector<int> &operator[](int k) {
		return next[k];
	}
};

int main() {
	string S;
	cin >> S;

	int m;
	cin >> m;

	int ans = 0;
	for (int ii = 0; ii < m; ii++) {
		string c;
		cin >> c;
		StringMatchingAutomaton am(c);

		int v = 0;
		for (char ch : S) {
			v = am[v][ch - 'A'];
			if (v == c.size()) ans++;
		}
	}
	cout << ans << endl;
}
0