hdu 2846 Repository

本文介绍了一种使用字典树(Trie)的数据结构来高效处理大量商品名称的搜索需求。通过预处理商品名称并构建字典树,能够快速响应用户输入的搜索条件,返回包含该字符串作为子串的商品数量。文章提供了完整的C++代码实现,包括插入商品名称到字典树、查询特定子串出现次数的功能。

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2846  

Repository

Description

When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.

Input

There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.

Output

For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.

Sample Input

20
ad
ae
af
ag
ah
ai
aj
ak
al
ads
add
ade
adf
adg
adh
adi
adj
adk
adl
aes
5
b
a
d
ad
s

Sample Output

0
20
11
11
2

字典树,g++ mle c++ ac。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<set>
using std::set;
using std::min;
using std::find;
using std::pair;
using std::vector;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 100010;
const int INF = 0x3f3f3f3f;
struct Node {
	Node *ch[26];
	int cnt;
	int f;
	Node() {
		rep(i, 26) ch[i] = NULL;
		cnt = 0;
		f = -1;
	}
}*root;
void insert(Node *x, char *str, int f) {
	int d = 0;
	char *p = str;
	while (*p != '\0') {
		d = *p - 'a';
		if (!x->ch[d]) x->ch[d] = new Node();
		x = x->ch[d];
		if (x->f != f) {
			x->f = f;
			x->cnt++;
		}
		p++;
	}
}
int query(Node *x, char *str) {
	char *p = str;
	while (*p != '\0') {
		if (!x || !x->ch[*p - 'a']) return 0;
		x = x->ch[*p - 'a'];
		p++;
	}
	return x->cnt;
}
void built(int t) {
	int n;
	char buf[100];
	rep(i, t) {
		scanf("%s", buf);
		n = strlen(buf);
		rep(j, n) {
			insert(root, buf + j, i);
		}
	}
}
void solve(int n) {
	int m;
	char buf[100];
	root = new Node();
	built(n);
	scanf("%d", &m);
	while (m--) {
		scanf("%s", buf);
		printf("%d\n", query(root, buf));
	}
}
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int n;
	while (~scanf("%d", &n)) {
		solve(n);
	}
	return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4792900.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值