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<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>#include<string>usingnamespacestd;voidhandleAttributes(vector<pair<string,string>>&attributes){std::stringattributeName;std::stringtemp;std::stringattributeValue;while(1){cin>>attributeName;cin>>temp;cin>>temp;attributeValue=temp.substr(1,temp.find_last_of('"')-1);attributes.push_back(make_pair(attributeName,attributeValue));if(temp.back()=='>')break;}}intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intn,q;cin>>n;cin>>q;vector<string>tags;vector<vector<pair<string,string>>>attributes;stringtemp;stringtag;stringprevTag;for(inti=0;i<n;i++){// parsing one line into the data//Searching for tag namecin>>tag;tag=tag.substr(1);//Checking if starting or endingif(tag[0]=='/')// ending{if(std::string::npos!=prevTag.find('.'))prevTag=prevTag.substr(0,prevTag.find_last_of('.'));elseprevTag="";continue;}else//starting tag{vector<pair<string,string>>attributesOfTag;if(tag.back()=='>'){tag.pop_back();}else{handleAttributes(attributesOfTag);}if(prevTag!=""){tag=prevTag+"."+tag;}tags.push_back(tag);prevTag=tag;attributes.push_back(attributesOfTag);}}stringtagName;stringattributeName;vector<string>results;for(inti=0;i<q;i++){cin>>tagName;attributeName=tagName.substr(tagName.find_last_of('~')+1);tagName=tagName.substr(0,tagName.find_last_of('~'));intj=0;boolfoundTag=false;boolfoundAttribute=false;for(constauto&tagIt:tags){if(tagIt==tagName){foundTag=true;for(constauto&attributeIt:attributes[j]){if(attributeIt.first==attributeName){results.push_back(attributeIt.second);foundAttribute=true;break;}}if(foundAttribute==false)results.push_back("Not Found!");break;}j++;}if(!foundTag)results.push_back("Not Found!");}for(constauto&result:results){cout<<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 →
Code: