Attribute Parser

Sort by

recency

|

469 Discussions

|

  • + 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;
    

    }

  • + 0 comments

    Thank you for sharing this wonderful post. I really like it—it’s truly excellent!

  • + 0 comments

    Awesome! This post is very informative and effective. I found exactly what I was looking for. Thank you!

  • + 0 comments

    Wonderful post! It’s engaging and full of valuable information. Thank you for sharing!

  • + 0 comments

    One important key is the tag name is actually not "tagxxx", it can be random stuff "a" like <abc attr1 = "adt1"> . Was kind of confused by example then realize from one of failed test case.

    Following are my approach

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <stack>
    #include <sstream>
    using namespace std;
    
    
    int main() {
        map<string, map<string, string>> tags;
        
        stack<string> currentTags;
        
        string input;
        getline(cin, input);
        stringstream initSS(input);
        int line{};
        int query{};
        initSS >> line >> query;
        
        string fullPath = "";
       
        for(int i = 0; i < line; i++) {
            getline(cin, input);
    
            // remove the tag
            input = input.substr(1, input.length() - 2);
                    
            stringstream ss(input);
            
            // work on tag
            string tag;
            ss >> tag;
            
            if(tag[0] == '/') {
                currentTags.pop();
                
                if(currentTags.size() > 0) {
                    fullPath = currentTags.top();
                }
                else {
                    fullPath.clear();
                }
                
                // skip to next line
                continue;
            }
            
        
                    if(currentTags.size() > 0) {
                        // add "." if already has path
                        fullPath.append(".");
                    }
                    fullPath.append(tag);
                    
                    // if no previous tag
                    currentTags.push(fullPath);
                    
                    if(tags.find(fullPath) == tags.end()) {
                        tags[fullPath] = map<string, string>();
                    }
       
                
            // work on attribute, get all remain as one single string
            string attr, eq, value;
            
            while(ss >> attr >> eq >> value) {
                if(value[0] == '"') {
                    value = value.substr(1, value.size() - 2);
                }
    
                      
                tags[fullPath][attr] = value;
            
            }
            
        }
        
        for(int i = 0; i < query; i++) {
            getline(cin, input);
                
                int index = input.find('~');
                string tag = input.substr(0, index);
                string query = input.substr(index + 1, input.size());
                        // indicate not found
                        if(tags.count(tag) && 
                        tags[tag].count(query)) {
                                    cout << tags[tag][query] << "\n";  
                            }
                        else {
                        cout << "Not Found!\n";
                        }
            
        }
        
        return 0;
    }