-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathattribute-parser.cpp
74 lines (64 loc) · 1.42 KB
/
attribute-parser.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
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
// https://www.hackerrank.com/challenges/attribute-parser/problem
int main() {
int n, q;
string tmp;
vector<string> s;
map<string, string> m;
cin >> n >> q;
// http://www.cplusplus.com/reference/istream/istream/ignore/
cin.ignore();
while (n--) {
getline(cin, tmp);
if (tmp[1] == '/') {
s.pop_back();
} else {
stringstream ss(tmp);
string buf;
int count = 0;
string key;
while (ss >> buf) {
// cout << buf << endl;
if (count == 0) {
buf.erase(0, 1);
if (buf[buf.size() - 1] == '>')
buf.erase(buf.size() - 1, buf.size());
s.push_back(buf);
} else {
if ((count - 1) % 3 == 0) key = buf;
else if ((count - 1) % 3 == 2) {
string key_prefix;
for (int i=0; i < s.size(); i++) {
if (i == 0) key_prefix = s[i];
else key_prefix += "." + s[i];
}
buf.erase(0, 1);
if (buf[buf.size() - 1] == '>')
buf.erase(buf.size() - 2, buf.size() - 1);
else
buf.erase(buf.size() - 1, buf.size());
m.insert(make_pair(key_prefix + "~" + key, buf));
}
}
count++;
}
}
}
while (q--) {
getline(cin, tmp);
map<string, string>::iterator it;
it = m.find(tmp);
if (it != m.end()) {
cout << m.at(tmp) << endl;
} else {
cout << "Not Found!" << endl;
}
}
return 0;
}