Attribute Parser

  • + 0 comments

    include

    include

    include

    include

    include

    include

    using namespace std;

    int main() { int n, q; cin >> n >> q; cin.ignore();

    map<string, map<string, string>> tagMap; // Stores tags and their attributes
    stack<string> tagStack; // Tracks current tag hierarchy
    
    for (int i = 0; i < n; ++i) {
        string line;
        getline(cin, line);
    
        if (line.substr(0, 2) == "</") {
            // Closing tag, pop from stack
            tagStack.pop();
        } else {
            // Parse opening tag
            stringstream ss(line);
            string tag, attr, eq, val;
            ss >> tag;
    
            tag = tag.substr(1); // Remove '<'
            if (tag.back() == '>') tag.pop_back(); // Remove '>' if no attributes
    
            // Current tag path
            string currentPath = tagStack.empty() ? tag : tagStack.top() + "." + tag;
            tagStack.push(currentPath);
    
            // Parse attributes
            while (ss >> attr) {
                ss >> eq >> val;
    
                // Handle attributes without > at the end
                if (val.back() == '>') val.pop_back();
                if (val.front() == '"' && val.back() == '"') {
                    val = val.substr(1, val.size() - 2); // Remove quotes
                }
                tagMap[currentPath][attr] = val;
            }
        }
    }
    
    for (int i = 0; i < q; ++i) {
        string query;
        getline(cin, query);
    
        size_t pos = query.find('~');
        string path = query.substr(0, pos);
        string attr = query.substr(pos + 1);
    
        if (tagMap.count(path) && tagMap[path].count(attr)) {
            cout << tagMap[path][attr] << endl;
        } else {
            cout << "Not Found!" << endl;
        }
    }
    
    return 0;
    

    }