結果

問題 No.430 文字列検索
ユーザー kurenai3110
提出日時 2016-10-03 00:16:41
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 1,646 ms / 2,000 ms
コード長 802 bytes
コンパイル時間 1,208 ms
コンパイル使用メモリ 65,544 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-10 00:06:43
合計ジャッジ時間 11,916 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

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