結果

問題 No.430 文字列検索
ユーザー CELICACELICA
提出日時 2020-11-03 08:30:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 1,119 bytes
コンパイル時間 1,556 ms
コンパイル使用メモリ 167,660 KB
実行使用メモリ 8,316 KB
最終ジャッジ日時 2023-09-29 14:25:08
合計ジャッジ時間 2,515 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 7 ms
8,316 KB
testcase_02 AC 4 ms
4,680 KB
testcase_03 AC 4 ms
4,680 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 8 ms
6,752 KB
testcase_12 AC 8 ms
7,308 KB
testcase_13 AC 8 ms
7,312 KB
testcase_14 AC 6 ms
6,264 KB
testcase_15 AC 5 ms
5,572 KB
testcase_16 AC 5 ms
5,536 KB
testcase_17 AC 5 ms
5,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

class Trie
{
public:
	int value;
	array<Trie*, 26> next;
	Trie() : value(0)
	{
		next.fill(nullptr);
	}
	void insert(const string s)
	{
		if (s[0] == '\0')
		{
			++this->value;
			return;
		}
		if (this->next[s[0] - BASE] == nullptr)
			this->next[s[0] - BASE] = new Trie();
		this->next[s[0] - BASE]->insert(s.substr(1));
	}
	bool find(const string s, int& count)
	{
		int countw{ count };
		for (auto it = s.begin(); it != s.end(); ++it)
		{
			Trie* cur = this;

			auto its = it;
			while (its != s.end() && cur)
			{
				cur = cur->next[*its - BASE];
				if (cur)
					count += cur->value;
				++its;
			}
		}

		return countw > count;
	}

private:
	const char BASE{ 'A' };
};

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

	string S;
	cin >> S;
	int M;
	cin >> M;
	vector<string> C(M);
	for (auto&& it : C)
		cin >> it;

	Trie* root = new Trie();
	for (const auto& it : C)
		root->insert(it);

	int cnt{ 0 };
	root->find(S, cnt);

	cout << cnt << "\n";

	return 0;
}
0