結果

問題 No.430 文字列検索
ユーザー 👑 jupirojupiro
提出日時 2020-10-24 20:27:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 2,815 bytes
コンパイル時間 1,496 ms
コンパイル使用メモリ 138,300 KB
実行使用メモリ 82,872 KB
最終ジャッジ日時 2023-09-28 20:47:41
合計ジャッジ時間 3,338 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 129 ms
82,052 KB
testcase_02 AC 20 ms
5,816 KB
testcase_03 AC 20 ms
5,904 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 126 ms
82,872 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 14 ms
13,424 KB
testcase_11 AC 62 ms
15,408 KB
testcase_12 AC 58 ms
15,676 KB
testcase_13 AC 65 ms
15,524 KB
testcase_14 AC 44 ms
15,668 KB
testcase_15 AC 38 ms
10,764 KB
testcase_16 AC 21 ms
5,872 KB
testcase_17 AC 21 ms
5,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353;
constexpr int MAX = 1100000;


//未verify
template<int char_size>
struct TrieNode {
	array<int, char_size> nxt;
	int cnt;
	vector<int> accept;
	TrieNode() :cnt(0) { fill(nxt.begin(), nxt.end(), -1); }
};

template<int char_size>
struct Trie {
	using Node = TrieNode<char_size>;
	vector<Node> nodes;
	int root;

	Trie() :root(0) { nodes.emplace_back(Node()); }
	void add(const vector<int>& str, int id) {
		int node_index = 0;
		for (auto& c : str) {
			if (nodes[node_index].nxt[c] == -1) {
				nodes[node_index].nxt[c] = (int)nodes.size();
				nodes.emplace_back(Node());
			}
			nodes[node_index].cnt += 1;
			node_index = nodes[node_index].nxt[c];
		}
		nodes[node_index].cnt += 1;
		nodes[node_index].accept.emplace_back(id);
	}
	void add(const vector<int>& str) { add(str, nodes[0].cnt); }

	int find(const vector<int>& str) {
		int node_index = 0;
		for (auto& c : str) {
			if (nodes[node_index].nxt[c] == -1) return -1;
			node_index = nodes[node_index].nxt[c];
		}
		return node_index;
	}
	int count() const { return nodes[0].cnt; }
	int size() const { return nodes.size(); }
};
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	string s; cin >> s;
	vector<int> v(s.size()); for (int i = 0; i < s.size(); i++) v[i] = s[i] - 'A';
	Trie<26> tr;
	for (int i = 0; i < v.size(); i++) {
		vector<int> t;
		for (int j = i; j < v.size() and j < i + 10; j++) {
			t.emplace_back(v[j]);
			tr.add(t);
		}
	}
	int m; cin >> m;
	int res = 0;
	for (int i = 0; i < m; i++) {
		string s; cin >> s;
		vector<int> t(s.size());
		for (int i = 0; i < s.size(); i++) t[i] = s[i] - 'A';
		int pos = tr.find(t);
		if (pos == -1) continue;
		res += tr.nodes[pos].accept.size();
	}
	cout << res << endl;
}
0