Attribute Parser

  • + 0 comments

    include

    include

    include

    include

    include

    include

    include

    using namespace std;

    int main(){ int num_of_lines{0}, num_of_queries{0}; stack lines; map new_map;

    cin >> num_of_lines >> num_of_queries;
    cin.ignore();
    
    string link_tag;
    for(int i{0}; i < num_of_lines; i++){
        string new_tag;
        getline(cin, new_tag);
        if (new_tag[1] == '/'){
            string temp = lines.top();
            lines.pop();
            link_tag = link_tag.substr(0, link_tag.size() - temp.size());
        }else{
            int num_equal = count(new_tag.begin(), new_tag.end(), '=');  
            if ( num_equal == 0){
                string name_tag = new_tag.substr(1, new_tag.size() - 2);
                lines.push(name_tag);
                link_tag += name_tag;
                continue;
            }
            int index_first_space = new_tag.find(' ');
            string name_tag = new_tag.substr(1, index_first_space - 1);
            lines.push(name_tag);
            link_tag += name_tag;
            new_tag.erase(0, index_first_space + 1);
            new_tag.erase(new_tag.size() - 1, 1);
            while (num_equal){
                int index_first_euqal = new_tag.find('=');
                string attribute = new_tag.substr(0, index_first_euqal -1);
                new_tag.erase(0, index_first_euqal + 3);
                // cout << "current tag: " << new_tag << '\n';
                int index_first_mark = new_tag.find("\"");    
                string value = new_tag.substr(0, index_first_mark);
                new_tag.erase(0, index_first_mark+2);       
                // cout << "current tag: " << new_tag << '\n';  
                new_map[link_tag + attribute] = value;
                num_equal--;
            }
        }
    }
    vector<string> queries(num_of_queries);
    for (int i{0}; i < num_of_queries; i++){
        string new_tag;
        getline(cin, new_tag);
        while(count(new_tag.begin(), new_tag.end(), '.')){
            int index_dot = new_tag.find('.');
            new_tag.erase(index_dot, 1);
        }
        int index_attri = new_tag.find('~');
        new_tag.erase(index_attri, 1);
        queries[i] = new_tag;
    }
    for (int i{0}; i < num_of_queries; i++){
        if (new_map.find(queries[i]) != new_map.end()){
            cout << new_map[queries[i]] << '\n';
        }else{
            cout << "Not Found!" << '\n';
        }
    
    }
    return 0;
    

    }