We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<iostream>#include<vector>#include<string>#include<map>#include<sstream>#include<iomanip>usingnamespacestd;classHRMLTree{private:HRMLTree*m_parent=nullptr;stringm_tag;map<string,string>m_attributes;vector<HRMLTree*>m_childs;public:voidsetTag(stringtag){m_tag=tag;}voidsetParent(HRMLTree*parent){m_parent=parent;}stringgetTag(){returnm_tag;}constvector<HRMLTree*>&getChilds(){returnm_childs;}HRMLTree*getParent(){returnm_parent;}voidaddChild(HRMLTree*tree){m_childs.push_back(tree);}stringfindAttribute(conststring&attribute_name){map<string,string>::iteratorit;if((it=m_attributes.find(attribute_name))==m_attributes.end())return"Not Found!";returnit->second;}voidparseHRMLTagLine(stringtag_line){tag_line=tag_line.substr(1,tag_line.size()-2);stringattributes=parseTag(tag_line);parseAttributes2(attributes);}stringparseTag(conststring&tag_line){intend=tag_line.find(' ');if(end==string::npos)end=tag_line.size();stringtag=tag_line.substr(0,end);setTag(tag);returntag_line.substr(end);}voidparseAttributes2(conststring&attributes){istringstreamiss(attributes);stringattribute_name,delimiter,attribute_value;while(iss>>attribute_name>>delimiter>>quoted(attribute_value)){m_attributes[attribute_name]=attribute_value;}}~HRMLTree(){for(HRMLTree*child:m_childs)deletechild;m_childs.clear();}};stringreadLine(){stringline;line.reserve(200);getline(cin,line);returnline;}voidreadHRMLTree(HRMLTree*parent,uint16_ttag_lines_count){while(tag_lines_count>0){stringtag_line=readLine();if(tag_line[1]=='/')// is ending tag?{parent=parent->getParent();}else{HRMLTree*child=newHRMLTree;child->parseHRMLTagLine(tag_line);child->setParent(parent);parent->addChild(child);parent=child;}tag_lines_count--;}}HRMLTree*findChild(HRMLTree*parent,conststring&tag){for(HRMLTree*child:parent->getChilds()){if(child->getTag()==tag)returnchild;}returnnullptr;}voidprintQueries(HRMLTree*parent,uint16_tquery_count){while(query_count>0){stringquery=readLine();// split attribute name and tagsinttilde_index=query.find('~');stringattribute_name=query.substr(tilde_index+1);stringtags=query.substr(0,tilde_index);// loop over tagsHRMLTree*parent_ref=parent;istringstreamiss(tags);stringtag;while(getline(iss,tag,'.')){parent_ref=findChild(parent_ref,tag);if(!parent_ref)break;}// print valuesif(!parent_ref)cout<<"Not Found!"<<endl;elsecout<<parent_ref->findAttribute(attribute_name)<<endl;query_count--;}}intmain(){uint16_ttag_lines_count,query_count;cin>>tag_lines_count>>query_count;cin.ignore();HRMLTree*tree=newHRMLTree;readHRMLTree(tree,tag_lines_count);printQueries(tree,query_count);deletetree;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Attribute Parser
You are viewing a single comment's thread. Return to all comments →