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.
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>usingnamespacestd;intmain(){map<string,map<string,string>>tags;stack<string>currentTags;stringinput;getline(cin,input);stringstreaminitSS(input);intline{};intquery{};initSS>>line>>query;stringfullPath="";for(inti=0;i<line;i++){getline(cin,input);// remove the taginput=input.substr(1,input.length()-2);stringstreamss(input);// work on tagstringtag;ss>>tag;if(tag[0]=='/'){currentTags.pop();if(currentTags.size()>0){fullPath=currentTags.top();}else{fullPath.clear();}// skip to next linecontinue;}if(currentTags.size()>0){// add "." if already has pathfullPath.append(".");}fullPath.append(tag);// if no previous tagcurrentTags.push(fullPath);if(tags.find(fullPath)==tags.end()){tags[fullPath]=map<string,string>();}// work on attribute, get all remain as one single stringstringattr,eq,value;while(ss>>attr>>eq>>value){if(value[0]=='"'){value=value.substr(1,value.size()-2);}tags[fullPath][attr]=value;}}for(inti=0;i<query;i++){getline(cin,input);intindex=input.find('~');stringtag=input.substr(0,index);stringquery=input.substr(index+1,input.size());// indicate not foundif(tags.count(tag)&&tags[tag].count(query)){cout<<tags[tag][query]<<"\n";}else{cout<<"Not Found!\n";}}return0;}
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 →
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