萌新刚学oi16pts求调
查看原帖
萌新刚学oi16pts求调
448018
KAqwq楼主2024/8/7 17:15
#include <bits/stdc++.h>
#define N 100005

typedef long long LL;
LL n, m, Trie[N * 30][65], tot, sum[N];

char s[N];

inline void insert() {
	LL len = strlen(s + 1), p = 1;
	for (int i = 1; i <= len; ++i) {
		LL ch = 0;
		if (s[i] >= 'A' && s[i] <= 'Z') ch = s[i] - 'A';
		else if (s[i] >= 'a' && s[i] <= 'z') ch = s[i] - 'a' + 26;
		else ch = s[i] - '0' + 52;
		if (!Trie[p][ch]) Trie[p][ch] = ++tot;
		p = Trie[p][ch];
		++sum[p];
	}
}
inline LL query() {
	LL len = strlen(s + 1), p = 1;
	for (int i = 1; i <= len; ++i) {
		LL ch = 0;
		if (s[i] >= 'A' && s[i] <= 'Z') ch = s[i] - 'A';
		else if (s[i] >= 'a' && s[i] <= 'z') ch = s[i] - 'a' + 26;
		else ch = s[i] - '0' + 52;
		if (!Trie[p][ch]) return 0;
		p = Trie[p][ch];
	}
	return sum[p];
}
LL T;
int main() {
	std::cin >> T;
	while (T--) {
		for (LL i = 1; i <= tot; ++i) for (LL j = 1; j <= 127; ++j) Trie[i][j] = 0;
		for (LL i = 1; i <= tot; ++i) sum[i] = 0;
		tot = 1;
		std::cin >> n >> m;
		for (int i = 1; i <= n; ++i) {
			scanf("%s", s + 1);
			insert();
		}
		for (int i = 1; i <= m; ++i) {
			scanf("%s", s + 1);
			std::cout << query() << '\n';
		}
	}
	return 0;
}
2024/8/7 17:15
加载中...