Attribute Parser

  • + 0 comments
    #include <bits/stdc++.h>
    using namespace std;
    
    unordered_map<string, string> m;
    string current_path;
    
    string cleanString(const string &s) {
        string temp;
        for (char c : s) {
            if (c != '\"' && c != '>') {
                temp.push_back(c);
            }
        }
        return temp;
    }
    
    void parseLine(const string &s) {
        istringstream stream(s);
        string temp, tag, attr, value;
    
        while (stream >> temp) {
            if (temp[0] == '<') {
                if (temp[1] == '/') {
                    size_t pos = current_path.find_last_of('.');
                    if (pos != string::npos) {
                        current_path = current_path.substr(0, pos);
                    } else {
                        current_path.clear();
                    }
                } else {
                    tag = temp.substr(1);
                    if (tag.back() == '>') tag.pop_back(); 
                    if (!current_path.empty()) {
                        current_path += "." + tag;
                    } else {
                        current_path = tag;
                    }
                }
            } else if (temp[0] == '=') {
                continue;
            } else if (temp[0] == '\"') {
                value = cleanString(temp);
                m[current_path + "~" + attr] = value;
            } else {
                attr = temp;
            }
        }
    }
    
    int main() {
        int n, q;
        cin >> n >> q;
        cin.ignore();
    
        while (n-- > 0) {
            string s;
            getline(cin, s);
            parseLine(s);
        }
    
        while (q-- > 0) {
            string query;
            getline(cin, query);
            if (m.find(query) != m.end()) {
                cout << m[query] << '\n';
            } else {
                cout << "Not Found!" << '\n';
            }
        }
        return 0;
    }