Attribute Parser

  • + 0 comments

    void insert_val(string &att, string &val, unordered_map &attr, vector &tags){

                string attribute = "";
        for(string t: tags){
            attribute += t + '.';
        }
        attribute.pop_back();
        attribute += '~' + att;
        attr[attribute] = val;
    }
    

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */

    vector<string> tags;
    unordered_map<string, string> attr;
    
    int n, q;
    cin>>n>>q;
    
    for(int i=0; i<n; i++){
        char c; cin>>c;
    
        if(cin.peek() == '/'){
            string s; cin>>s;
            tags.pop_back();
        }else{
            string tag; cin>>tag;
    
    
            if(tag.back() == '>'){
                tag.pop_back();
                tags.push_back(tag);
            }else{
                tags.push_back(tag);
                while(true){
                string att, eq, val;
                cin>>att>>eq>>val;
    
                if(val.back() == '>'){
                    val.pop_back();
                    val.pop_back();
                    val = val.substr(1);
                    insert_val(att, val, attr, tags);
                    break;
                }else{
                    val.pop_back();
                    val = val.substr(1);
                    insert_val(att, val, attr, tags);
                }
            }
        }
    
    
        }
    }
    
    for(int i=0; i<q; i++){
        string Q; cin>>Q;
        if(attr.find(Q) != attr.end()){
            cout<<attr[Q]<<endl;
        }else{
            cout<<"Not Found!"<<endl;
        }
    }
    
    return 0;
    

    }