-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathapp_dev.cpp
119 lines (93 loc) · 2.06 KB
/
app_dev.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <bits/stdc++.h>
#define ALPHABET 26
using namespace std;
struct trieNode
{
struct trieNode* child[ALPHABET];
bool wordEnding;
int prio;
};
void insertNode(struct trieNode* root, string key, int prio);
struct trieNode* createNode(void);
int anotherSearch(struct trieNode* root, string key);
vector<string> matching;
int maxPrio;
int main(void)
{
int n, q;
cin >> n >> q;
struct trieNode *root = createNode();
unordered_map<string, int> db;
for (int i = 0; i < n; i++)
{
string s;
int p;
cin >> s >> p;
db[s] = p;
insertNode(root, s, p);
}
for (int i = 0; i < q; i++)
{
// vector<string> temp;
// matching = temp;
maxPrio = 0;
string qe;
cin >> qe;
// moddedSearch(root, qe);
int retVal = anotherSearch(root, qe);
// for (auto s : matching)
// if (db[s] > retVal)
// retVal = db[s];
// if (maxPrio != 0)
// cout << maxPrio << endl;
// else
// cout << "-1" << endl;
if (retVal == 0)
cout << "-1" << endl;
else
cout << retVal << endl;
}
return 0;
}
void insertNode(struct trieNode* root, string key, int prio)
{
struct trieNode* temp = root;
for (int i = 0; i < key.size(); i++)
{
int index = key[i] - 'a';
if (!temp -> child[index])
temp -> child[index] = createNode();
temp = temp -> child[index];
if (temp -> prio < prio)
temp -> prio = prio;
}
temp -> wordEnding = true;
temp -> prio = prio;
}
struct trieNode* createNode(void)
{
struct trieNode* hello = new trieNode;
hello -> wordEnding = false;
hello -> prio = 0;
for (int j = 0; j < ALPHABET; j++)
hello -> child[j] = NULL;
return hello;
}
int anotherSearch(struct trieNode* root, string key)
{
struct trieNode* temp = root;
int max_prio_seen = 0;
for (int i = 0; i < key.length(); i++)
{
int index = key[i] - 'a';
if (!temp -> child[index])
{
max_prio_seen = 0;
return max_prio_seen;
}
else
temp = temp -> child[index];
max_prio_seen = temp -> prio;
}
return max_prio_seen;
}