forked from MahadevGopanpalli/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttribute_parser.cpp
More file actions
77 lines (64 loc) · 2.16 KB
/
Attribute_parser.cpp
File metadata and controls
77 lines (64 loc) · 2.16 KB
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
vector<string> tag_stack; //tag1 tag2
map<string, string> attrs;
void insert_attr(string & name, string & val) {
string full;
for(string & str : tag_stack)
full += str + "."; //tag1.tag2.
full.pop_back(); //tag1.tag2
full += "~" + name; //tag.tag2~name
attrs[full] = val;
}
int main() {
int n, q;
cin >> n >> q;
for(int i = 0; i < n; ++i) {
char c; cin >> c; // taking single character <
if(cin.peek() == '/') {
string cn; cin >> cn;
tag_stack.pop_back();
}
else {
string name;
cin >> name; //taking tag name
if(name.back() == '>') { //<tag1>
name.pop_back(); // tag1> => tag1
tag_stack.push_back(name); //adding tag name int vector
}
else {
tag_stack.push_back(name); // adding tag name int vector
for(;;) {
string attr_name, attr_val, eq;
cin >> attr_name >> eq >> attr_val; //taking att value & att name
if(attr_val.back() == '>') { //"HelloWorld">
attr_val.pop_back(); //"HelloWorld"
attr_val.pop_back(); //"HelloWorld
attr_val = attr_val.substr(1); // HelloWorld
insert_attr(attr_name, attr_val);
break;
}
else {
attr_val.pop_back();
attr_val = attr_val.substr(1);
insert_attr(attr_name, attr_val);
}
}
}
}
}
for(int i = 0; i < q; ++i) {
string quer;
cin >> quer;
if(attrs.find(quer) != attrs.end())
cout << attrs[quer] << endl;
else
cout << "Not Found!" << endl;
}
return 0;
}