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.
I used a stack to build the valid paths and a hash-map to store the attributes values for every valid path found.
#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>#include<list>#include<map>#include<sstream>usingnamespacestd;intmain(){intN,Q;std::map<string,string>valid_paths;cin>>N>>Q;// Build the valid paths while processingstringtag;list<string>current_path;std::getline(std::cin,tag);while(N--){std::getline(std::cin,tag);// If it's a openning tag, append the name to the // current path with the delimiter char '.' and// process the attributesif(tag[1]!='/'){if(!current_path.empty()){current_path.push_back(".");}std::istringstreamiss(tag);std::stringtoken;while(std::getline(iss,token,' ')){if(token[0]=='<'){// It's the tag namecurrent_path.push_back(token.substr(1,token.find_last_of('>')-1));}else{// It's a attribute name// Flatten the current path and add it to the map// of the valid pathsstd::stringatrr_name=token;std::stringatrr_value;std::stringvalid_path;for(strings:current_path){valid_path+=s;}std::getline(iss,token,' ');// Get the '='std::getline(iss,token,' ');// Get the attr-valueatrr_value=token.substr(1,token.find_last_of('"')-1);valid_path+="~"+atrr_name;valid_paths[valid_path]=atrr_value;}}// If it's a closing tag, remove the tag name// from the current path (and the '.' if necessary) }else{current_path.pop_back();if(!current_path.empty()){current_path.pop_back();}}}while(Q--){stringquery;cin>>query;stringquery_result=valid_paths[query];if(query_result.empty()){query_result="Not Found!";}cout<<query_result<<endl;}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 →
I used a stack to build the valid paths and a hash-map to store the attributes values for every valid path found.