Attribute Parser

  • + 0 comments

    int main() { unordered_map> map;

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    //number of lines and number of querries
    int N, Q;
    smatch m;
    regex r_tag(R"(<(\w+))");
    regex r_tag_end(R"(</(\w+)>)");
    regex r_att(R"((\w+) = \"([^"]+)\")");
    regex r_q(R"((.+)~(\w+))");
    string lineage = ""; 
    cin >> N >> Q;
    for(auto i = 0; i <= N; ++i){
        string line; 
        getline(cin, line);
        //cout<<i<<"\n";
        //cout<<line<<"\n"; 
        regex_search(line, m, r_tag);
        if (m[1].matched){
            if (lineage.size()==0){
                lineage += string(m[1]);
            }
            else{
                lineage+="."+string(m[1]); 
            }
            unordered_map<string, string> att;
            //cout<<lineage<<"\n";
    
            auto begin = sregex_iterator(line.begin(), line.end(), r_att);
            auto end = sregex_iterator();
            for (auto it = begin; it != end; ++it) {
                smatch match = *it;
                //cout << "      att: " << match[1] << "=" << match[2] << "\n";
                att[string(match[1])] = string(match[2]);
            }
            map[lineage] = att;
        }
        regex_search(line, m, r_tag_end);
        if (m[1].matched){
            //cout<<line<<"\n";
            string rep = "\\.?"+string(m[1]);
            //string rep = "R("+string(m[1])+"\")";
            //cout<<rep<<"\n";
            regex rep_pat(rep);
            //cout<<"Lineage before "<<lineage<<"\n";
            string nlineage = regex_replace(lineage,rep_pat , "");
            lineage = nlineage;
            //cout<<lineage<<"\n";
        }
    }
    for(auto i = 0; i < Q; ++i){
        string query; 
        getline(cin, query);
        //cout<<"query: "<< query << "\n";
        regex_search(query, m, r_q);
        //cout << "Found  " << m[1] << "=" << m[2] << "\n";
        if(m[1].matched){
            string key = string(m[1]);
            //cout<<"key  "<<key<<"\n";
            if(map.find(key) != map.end()){
                string key1 = string(m[2]);
                //cout<<"key  "<<key<<"\n";
                //cout<<"key1 "<<key1<<"\n";
                if (map[key].find(key1) != map[key].end()){
                    cout<<map[key][key1]<<"\n";
                }else{
                    cout<<"Not Found!\n";
                }
            }else{
                cout<<"Not Found!\n";
            }
        }else{
            cout<<"Not Found!\n";
        }
    }
    return 0;
    

    }