此代码在代码块和geeks4geeks IDE中运行,但在hackerearth上显示SIGSEGV错误: 这是问题的链接:https://www.hackerearth.com/practice/data-structures/advanced-data-structures/trie-keyword-tree/tutorial/#c183497 我已经尝试了大多数输入,并在代码块中给出了正确的输出。
#include <bits/stdc++.h>
using namespace std;
ofstream f1("data.out");
ifstream g("data.in");
struct tnode {
tnode * a[26];
int w;
};
struct tnode * getnode() {
struct tnode * t = new tnode;
t - > w = 0;
for (int i = 0; i < 26; i++) {
t - > a[i] = NULL;
}
};
void insert1(struct tnode * root, string s, int w1) {
struct tnode * pcrawl = root;
int len = s.length(), index;
for (int i = 0; i < len; i++) {
index = s[i] - 'a';
if (pcrawl - > a[index] == NULL) {
pcrawl - > a[index] = getnode();
}
pcrawl = pcrawl - > a[index];
if (w1 > pcrawl - > w)
pcrawl - > w = w1;
}
}
int find1(struct tnode * root, string s) {
struct tnode * pcrawl = root;
int len = s.length(), index;
for (int i = 0; i < len; i++) {
index = s[i] - 'a';
if (pcrawl - > a[index] != NULL) {
pcrawl = pcrawl - > a[index];
} else return -1;
}
if (pcrawl != NULL)
return pcrawl - > w;
}
int main() {
int n, q, w;
struct tnode * root = getnode();
string s;
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> s >> w;
insert1(root, s, w);
}
for (int i = 0; i < q; i++) {
cin >> s;
cout << find1(root, s) << "\n";
}
}