結果

問題 No.430 文字列検索
ユーザー snrnsidysnrnsidy
提出日時 2021-03-07 21:49:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,878 ms / 2,000 ms
コード長 1,247 bytes
コンパイル時間 1,744 ms
コンパイル使用メモリ 125,340 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 11:33:36
合計ジャッジ時間 15,511 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 394 ms
5,376 KB
testcase_02 AC 855 ms
5,376 KB
testcase_03 AC 714 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 1,814 ms
5,376 KB
testcase_12 AC 1,812 ms
5,376 KB
testcase_13 AC 1,808 ms
5,376 KB
testcase_14 AC 1,878 ms
5,376 KB
testcase_15 AC 1,627 ms
5,376 KB
testcase_16 AC 953 ms
5,376 KB
testcase_17 AC 867 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> 
#include <iomanip>
#include <unordered_map>
#include <memory.h>
#include <unordered_set>
#include <fstream>

using namespace std;

string a, b;
vector <int> pi;
vector <int> res;

void kmp()
{
	res.clear();
	int j = 0;
	for (int i = 0; i < a.length(); i++)
	{
		while (j > 0 && a[i] != b[j])
		{
			j = pi[j - 1];
		}
		if (a[i] == b[j])
		{
			if (j == b.length() - 1)
			{
				res.push_back(i - b.length() + 1);
				j = pi[j];
			}
			else
			{
				j++;
			}
		}
	}
}

void getpi()
{
	pi.clear();
	pi.resize(b.length());
	int j = 0;
	for (int i = 1; i < b.length(); i++)
	{
		while (j > 0 && b[i] != b[j])
		{
			j = pi[j - 1];
		}
		if (b[i] == b[j])
		{
			pi[i] = ++j;
		}
	}
}

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	long long int ans = 0;
	int n;
	cin >> a;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> b;
		getpi();
		kmp();
		ans += res.size();
	}

	cout << ans << '\n';

	return 0;
}
0