-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
360 lines (301 loc) · 9.95 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio> // For freopen
#include <map>
using namespace std;
class Node {
public:
string data;
vector<Node*> children;
void add_child(Node* node);
bool has_child(const string tag_name);
Node* get_prev(Node* node);
Node* get_next(Node* node);
Node* get_last(Node* node);
Node* get_node(int index);
bool remove_node(const string& data);
// Constructor
Node(const std::string& value) : data(value) {}
// Deep copy constructor
Node(const Node& other) {
data = other.data;
// Recursively copy all children
for (Node* child : other.children) {
children.push_back(new Node(*child)); // Deep copy
}
}
};
bool Node::has_child(const string tag_name) {
for (Node* child : children) {
if (child->data == tag_name) {
return true; // Child with the specified data found
}
}
return false; // No child with the specified data found
}
void Node::add_child(Node* node) {
children.push_back(node);
}
Node* Node::get_last(Node* node) {
return children[children.size() -1];
}
Node* Node::get_node(int index) {
if (index >= 0 && static_cast<size_t>(index) < children.size()) {
return children[index];
}
return nullptr; // Return nullptr if index is out of bounds
}
Node* Node::get_prev(Node* node) {
Node* prev_child = nullptr;
Node* current_child = nullptr;
for (Node* child : children) {
prev_child = current_child;
if (node->data == child->data) {
current_child = node;
}
}
return prev_child;
}
Node* Node::get_next(Node* node) {
Node* next_child = nullptr;
Node* current_child = nullptr;
int i = 0;
for (Node* child : children) {
if (node->data == child->data) {
current_child = node;
if (children.size() > i) {
next_child = children[ i + 1];
}
}
i++;
}
return next_child;
}
bool Node::remove_node(const string& data) {
// Iterate through the list of children
for (auto it = children.begin(); it != children.end(); ++it) {
if ((*it)->data == data) {
delete *it;
children.erase(it);
return true;
}
}
return false;
}
class HRML_Parser {
private:
vector<Node*> node_tree; // contains all vectors of tags
map<string, map<string, string> > node_map;
Node* root_node = nullptr;
private:
string get_tag_name(const string& node);
// map_node_attributes
size_t create_node_map(string current_tag, string node, size_t starting_pos) ;
void remove_character(string& string_to_update, const char char_to_remove);
bool validate_query(const string& query);
vector<string> split_string(const string& str, char delimiter);
public:
void build_node_tree(const string& hrml_node);
void build_attribute_map(const string& hrml_node);
void handle_queries(const string& query);
};
void HRML_Parser::remove_character(string& string_to_update, const char char_to_remove) {
string_to_update.erase(remove(string_to_update.begin(), string_to_update.end(), char_to_remove), string_to_update.end());
}
void HRML_Parser::build_node_tree(const string& hrml_node)
{
string tag_name = get_tag_name(hrml_node);
Node* current_node = new Node(tag_name);
// There is not root node
if (root_node == nullptr) {
root_node = current_node;
} else {
// if a tag is closed which is not the root tag
if (hrml_node.substr(0, 2) == "</" && root_node->data == tag_name) {
// we only have the root node that is closed now, so we need to add it to the node_tree
if (node_tree.size() == 0) {
node_tree.push_back(new Node(*root_node));
}
root_node = nullptr;
// the root node is closed, so we null it
} else if (hrml_node.substr(0, 2) == "</" && tag_name == root_node->get_last(current_node)->data) {
node_tree.push_back(new Node(*root_node)); // contains a, b
root_node->remove_node(current_node->data);
} else {
root_node->add_child(current_node);
}
}
}
void HRML_Parser::build_attribute_map(const string& hrml_node)
{
string tag_name = get_tag_name(hrml_node);
size_t current_end_position = hrml_node.find(" ", 0);
/**
* Loops through all attributes in a specific node
*/
bool node_parsed_done = false;
int attributes_parsed = 0;
while (node_parsed_done == false) {
attributes_parsed++;
current_end_position = create_node_map(tag_name, hrml_node, current_end_position + 1);
if (current_end_position == string::npos) {
node_parsed_done = true;
return;
} else if (current_end_position + attributes_parsed >= hrml_node.length()) {
node_parsed_done = true;
}
}
}
void HRML_Parser::handle_queries(const string& query) {
if (query.find('~') == string::npos) {
cout << "Invalid query!!" << endl;
return;
}
if (!validate_query(query)) {
cout << "Not Found!" << endl;
return;
}
size_t tag_start = query.find_last_of('.');
size_t tag_end = query.find_first_of("~");
if (tag_start == string::npos) {
// e.g tag1~name
tag_start = 0;
} else {
tag_start = tag_start +1;
}
tag_end = tag_end +1;
string tag = query.substr(tag_start, tag_end - 1 - tag_start);
string attribute = query.substr(tag_end, query.length());
map<string, string> node_map_tag = node_map[tag];
auto iter = node_map_tag.find(attribute);
if (iter != node_map_tag.end()) {
// Key exists, access the value
cout << iter->second << endl;
} else {
// Key does not exist
cout << "Not Found!" << endl;
}
}
/***
* Private funcs
*/
string HRML_Parser::get_tag_name(const string& hrml_node)
{
string tag_name;
size_t tag_end = hrml_node.find_first_of(" ", 0);
if (tag_end == string::npos) {
tag_end = hrml_node.find(">", 0);
}
if (tag_end == string::npos) {
cerr << "Cannot parse tag" << endl;
return "";
}
tag_end = tag_end - 1;
tag_name = hrml_node.substr(0, 2) == "</" ? hrml_node.substr(2, tag_end) : hrml_node.substr(1, tag_end);
remove_character(tag_name, '>');
remove_character(tag_name, ' ');
return tag_name;
}
size_t HRML_Parser::create_node_map(string current_tag, string node, size_t starting_pos) {
size_t equal_sign_pos = node.find("=", starting_pos);
if (equal_sign_pos == string::npos) {
return string::npos;
}
string attribute_name = node.substr(starting_pos, equal_sign_pos - starting_pos); // another
size_t quote_start_pos = node.find('"', equal_sign_pos);
size_t quote_end_pos = node.find('"', quote_start_pos + 1);
if (quote_start_pos == string::npos || quote_end_pos == string::npos) {
return string::npos;
}
quote_start_pos = quote_start_pos + 1;
string attribute_value = node.substr(quote_start_pos, quote_end_pos - quote_start_pos); // foo
remove_character(attribute_name, ' ');
remove_character(attribute_value, '"');
node_map[current_tag][attribute_name] = attribute_value;
return quote_end_pos;
}
/*
*
* b~value
* a.b~size
* a.b~value
*/
bool HRML_Parser::validate_query(const string& query) {
size_t tag_start = query.find('.');
size_t tag_end = query.find_first_of("~");
vector<string> tags;
string current_tag;
// this can be put into a method - get tags from query
if (tag_start == string::npos) {
// e.g tag1~name
tag_start = 0;
current_tag = query.substr(tag_start, tag_end);
tags.push_back(current_tag);
} else {
// a.c.d.e~strength
tag_start = 0;
current_tag = query.substr(tag_start, tag_end);
tags = split_string(current_tag, '.');
}
for(Node* node : node_tree) {
if(node->data == tags[0]) {
int index = 0;
for(string tag : tags) {
// skip first tag
if (tag != tags[0]) {
if (node->get_node(index -1) == nullptr || node->get_node(index -1)->data != tag) {
break;
}
}
index++;
// all tags found !
if (index == tags.size()) {
return true;
}
}
}
}
return false;
}
vector<string> HRML_Parser::split_string(const string& str, char delimiter) {
vector<string> result;
string temp = "";
for (char ch : str) {
if (ch == delimiter) {
if (!temp.empty()) {
result.push_back(temp); // Add substring to the result
temp = ""; // Reset temp string
}
} else {
temp += ch; // Build the substring
}
}
// Add the last substring (if any)
if (!temp.empty()) {
result.push_back(temp);
}
return result;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int lines, queries;
cin >> lines >> queries;
cin.ignore();
HRML_Parser parser;
string hrml_node;
string query_request;
for (int i = 0; i < lines; ++i) {
getline(cin, hrml_node);
parser.build_node_tree(hrml_node);
parser.build_attribute_map(hrml_node);
}
for (int i = 0; i < queries; i++) {
getline(cin, query_request);
// the node tree seems correct. We just need to get the query, travers each node_tree and the
// containing node vectors in the order
parser.handle_queries(query_request);
}
return 0;
}