求助,样例能过,但是测试点一个不过
查看原帖
求助,样例能过,但是测试点一个不过
1165047
Koishi514ww楼主2024/11/20 18:59
#include<bits/stdc++.h>
using namespace std;
const int N = 3e6 + 5;
int trie[N][52], cnt, exist[N];
void insert(string& s) {
	int p = 0;
	for (int i = 0; i < s.size(); i++) {
		int c = s[i] >= 'A' ? s[i] - 'A' + 26 : s[i] - 'a';
		if (!trie[p][c]) trie[p][c] = ++cnt;
		p = trie[p][c];
		exist[p]++;
	}
}
void query(string& s) {
	int p = 0;
	for (int i = 0; i < s.size(); i++) {
		int c = s[i] >= 'A' ? s[i] - 'A' + 26 : s[i] - 'a';
		if (!exist[p]&&p) {
			cout << 0 << endl;
			return;
		}
		p = trie[p][c];
	}
	cout << exist[p] << endl;
}
void clear() {
	for (int i = 0; i <= cnt; i++) {
		for (int j = 0; j < 52;j++) {
			trie[i][j] = 0;
		}
	}
	for (int i = 0; i <= cnt; i++) exist[i] = 0;
	cnt = 0;

}
void koishi() {
	int n, q; cin >> n >> q;
	for (int i = 0; i < n; i++) {
		string s; cin >> s;
		insert(s);
	}
	for (int i = 0; i < q; i++) {
		string s; cin >> s;
		query(s);
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin >> t;
	while (t--) {
		koishi();
		clear();
	}
}
2024/11/20 18:59
加载中...