結果

問題 No.430 文字列検索
ユーザー CELICACELICA
提出日時 2020-10-18 22:45:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 942 bytes
コンパイル時間 1,730 ms
コンパイル使用メモリ 174,904 KB
実行使用メモリ 7,708 KB
最終ジャッジ日時 2024-11-10 00:48:15
合計ジャッジ時間 2,830 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ul = unsigned long;
using ull = unsigned long long;

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

	string s;
	cin >> s;

	ull ls = s.size();

	vector<vector<ull> > hs(11);
	for (ull i = 1; i <= min(ls, 10ull); ++i)
	{
		ull h{ 0 };
		for (ull j = 0; j < i; ++j)
			h = h * 26 + s[j] - 'A';

		hs[i].push_back(h);
		ull d = (ull)pow(26, i - 1);
		for (ull j = 1; j <= ls - i; ++j)
		{
			h %= d;
			h *= 26;
			h += s[j + i - 1] - 'A';
			hs[i].push_back(h);
		}

		sort(hs[i].begin(), hs[i].end());
	}

	int m;
	cin >> m;

	int res{ 0 };
	for (int i = 0; i < m; ++i)
	{
		string c;
		cin >> c;

		ull h{ 0 };
		for (ull i = 0; i < c.size(); ++i)
			h = h * 26 + c[i] - 'A';

		auto itb = hs[c.size()].begin();
		auto ite = hs[c.size()].end();

		res += upper_bound(itb, ite, h)
			- lower_bound(itb, ite, h);
	}

	cout << res << "\n";

	return 0;
}
0