結果

問題 No.430 文字列検索
ユーザー kurenai3110kurenai3110
提出日時 2016-10-03 00:16:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,829 ms / 2,000 ms
コード長 802 bytes
コンパイル時間 558 ms
コンパイル使用メモリ 65,828 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 11:04:53
合計ジャッジ時間 13,208 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 544 ms
6,940 KB
testcase_02 AC 755 ms
6,944 KB
testcase_03 AC 519 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 5 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 1,688 ms
6,940 KB
testcase_12 AC 1,689 ms
6,940 KB
testcase_13 AC 1,688 ms
6,940 KB
testcase_14 AC 1,829 ms
6,940 KB
testcase_15 AC 1,594 ms
6,940 KB
testcase_16 AC 837 ms
6,940 KB
testcase_17 AC 781 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
int ans;

int table[50001];

void kmp_maketable(string key)
{
	table[0] = -1; table[1] = 0;
	int i = 2, j = 0;
	while (i == 0 || key[i - 1]) {
		if (key[i - 1] == key[j]) {
			table[i++] = ++j;
		}
		else if (j>0) {
			j = table[j];
		}
		else {
			table[i++] = 0;
		}
	}
}
void kmp_search(string str, string key)
{
	int i = 0, j = 0;
	while (str[i]) {
		if (str[i] == key[j]) {
			if (!key[j + 1]) {
				ans++;
			}
			i++, ++j;
		}
		else if (j>0) {
			j = table[j];
		}
		else {
			i++;
		}
	}
}

int main()
{
	string s;
	cin >> s;
	int m; cin >> m;

	for (int i = 0; i < m; i++) {
		string target; cin >> target;
		kmp_maketable(target);
		kmp_search(s, target);
	}

	cout << ans << endl;
	return 0;
}
0