-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute-parser.cpp
More file actions
58 lines (47 loc) · 1.48 KB
/
Copy pathattribute-parser.cpp
File metadata and controls
58 lines (47 loc) · 1.48 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
#include <iostream>
#include <sstream>
#include <map>
#include <stack>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
cin.ignore(); // Ignore newline after numbers
map<string, string> attributes;
stack<string> tagStack;
// Read HRML input
for (int i = 0; i < n; i++) {
string line;
getline(cin, line);
stringstream ss(line);
string word;
ss >> word;
if (word[1] == '/') {
// Closing tag (e.g., </tag1>)
tagStack.pop();
} else {
// Opening tag (e.g., <tag1 name="value">)
word = word.substr(1); // Remove '<'
if (word.back() == '>') word.pop_back(); // Remove '>'
string currentTag = tagStack.empty() ? word : tagStack.top() + "." + word;
tagStack.push(currentTag);
string attr, eq, value;
while (ss >> attr >> eq >> value) {
if (value.back() == '>') value.pop_back(); // Remove '>'
value = value.substr(1, value.length() - 2); // Remove quotes
attributes[currentTag + "~" + attr] = value;
}
}
}
// Process Queries
for (int i = 0; i < q; i++) {
string query;
getline(cin, query);
if (attributes.find(query) != attributes.end()) {
cout << attributes[query] << endl;
} else {
cout << "Not Found!" << endl;
}
}
return 0;
}