Attribute Parser

  • + 0 comments

    Got it worked,

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <string>
    using namespace std;
    
    // struct Node{
    //     map<string,string> attr_map;
    //     map<string,Node> childMap;
    //     Node * parent = nullptr;
    //     string tag_name;
    // };
    
    string getHash(const vector<string> & vec){
        string hash  = "";
        for (auto c:vec){
            hash += c+".";
        }
        return hash;
    }
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        uint32_t N,Q;
        cin >> N >> Q;
        vector <string> stack; 
        bool listing_flg = true;
        
        map<string,map<string,string>> tagMap;
        
        for (int i=0;i<N;i++){
            
            string s;
            cin >> s;
            // string hash_ = getHash(stack);
            // cout << "|" << hash_ <<"\n";
            
            // Determine Starting Tag or Ending Tag
            if (s.find("/") != string::npos) {
                // Go to the next line if this is a end tag
                // string tag_name = s.substr(2, s.length()-3);
                
                // auto tail = stack.back();
                stack.pop_back();
                
            } else {
                // Starting Tag. Scan attributes
                map<string,string> attr_map;
                string hash_prefix = getHash(stack);
                if (s.find(">")==string::npos){
                    s = s.substr(s.find("<")+1);
                    tagMap[hash_prefix + s] = attr_map; 
                    stack.push_back(s);          
                } else {
                    // no attribute for this tag, skip following
                    auto start_pos = s.find("<"); 
                    auto end_pos = s.find(">"); 
                    tagMap[hash_prefix + s.substr(start_pos+1, end_pos-start_pos-1)] = attr_map;
                    stack.push_back(s.substr(start_pos+1, end_pos-start_pos-1)); 
                    continue;
                }
    
                string attr;
                cin >> attr;
                // ">" is end of the line
                bool val_flg = false;
                string attr_name;
                string attr_value;
                while (attr.find(">") == string::npos){
                    if (attr.compare("=")==0) {
                        attr.clear();
                        cin >> attr;
                        continue;
                    } else if (attr.find(">") != string::npos){
                        auto delimit_pos = attr.find("=");
                        attr_name = attr.substr(0, delimit_pos-1);
                        attr_value = attr.substr(delimit_pos+1, attr.length() - attr_name.length() - 2);
                        tagMap[hash_prefix+s][attr_name] = attr_value;
                        val_flg = false;
                    } else {
                        if (val_flg){
                            val_flg = false;                         
                            attr_value = attr.substr(1,attr.length()-2);
                            tagMap[hash_prefix+s][attr_name] = attr_value;
                        } else {
                            val_flg = true;
                            attr_name = attr;
                        }
                    }
                    attr.clear();
                    cin >> attr;    
                }
                
                // handle the last value
                attr_value = attr.substr(1,attr.length()-3);
                tagMap[hash_prefix+s][attr_name] = attr_value;
            }
        }
    
        //// Read query
        for (int i=0; i< Q; i++){
            string q;
            cin >> q;
            auto tilde_pos = q.find("~");
            auto hash_key = q.substr(0,tilde_pos);
            auto attr_name = q.substr(tilde_pos+1, q.length() - hash_key.length() - 1);
            
            // cout << hash_key << "|" << attr_name << '\n';
            if (tagMap.find(hash_key)==tagMap.end()){
                cout<< "Not Found!" << endl;
            } else {
                auto it = tagMap.find(hash_key);
                if (it->second.find(attr_name)==it->second.end()){
                    cout << "Not Found!" <<endl;
                } else {
                    auto it2 = it->second.find(attr_name);
                    cout << it2->second <<endl;
                    
                }
            }
        }
    
        return 0;
    }