rt.
我真的不理解,最后一个点AC是不是表示整体的逻辑没问题?
但找不到RE的原因。
求调,感激不尽
马蜂有点怪。希望您能原谅。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct Node {
char data;
Node* next[60];
int down_cnt;
};
typedef Node* Trie;
int trans(char ch) {
if (ch >= 'A' && ch <= 'Z')
return ch - 'A';
return ch - 'a' + 26;
}
Trie init_Trie() {
Trie trie = new Node;
trie->data = '\0';
trie->down_cnt = 0;
memset(trie->next, 0, sizeof(trie->next));
return trie;
}
void add(const char* str, Trie trie) {
Trie ptr = trie;
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (ptr->next[trans(str[i])] == nullptr) {
ptr->next[trans(str[i])] = init_Trie();
ptr->next[trans(str[i])]->data = trans(str[i]);
}
ptr->down_cnt++;
ptr = ptr->next[trans(str[i])];
}
}
void dfs(const char* str, int p, int len, Trie &trie) {
if (p == len - 1) {
cout << trie->down_cnt << endl;
return;
}
if(trie->next[trans(str[p])]==nullptr)
{
cout<<0<<endl;
return ;
}
dfs(str, p + 1, len, trie->next[trans(str[p])]);
}
void free_Trie(Trie trie) {
if (trie == nullptr) {
return;
}
for (int i = 0; i < 60; i++) {
free_Trie(trie->next[i]);
}
delete trie;
}
int main() {
int t, n, q;
string str;
cin >> t;
while (t--) {
Trie trie = init_Trie();
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> str;
add(str.c_str(), trie);
}
for (int i = 0; i < q; i++) {
cin >> str;
dfs(str.c_str(), 0, strlen(str.c_str()), trie);
}
free_Trie(trie);
}
return 0;
}