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<stack>#include<map>#include<string>usingnamespacestd;classHRML{private://VariablesconststringopenAnglebracket="<";conststringcloseAnglebracket=">";conststringcloseTag="</";stack<string>tagStack;map<string,vector<pair<string,string>>>mapTagNameAttribute;intnoOfLines=0;intnoOfQueries=0;//MethodsvoidUpdateTagStack(stringtagName);voidUpdateTagAttributes(stringtagName,stringstringAfterTagname);vector<string>ParseTagAttributes(stringstringAfterTagname);pair<string,string>GetAttributeNameValuePair(stringstringAfterTagname);size_tParseOpenAnglebracket(stringline);conststringGetTagName();stringTrim(stringstr,constchar*t=" \t\n\r\f\v");public://MethodsHRML(intN,intQ){noOfLines=N;noOfQueries=Q;}voidParseLines();voidParseQueries();};intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intN,Q;cin>>N>>Q;HRMLhrml(N,Q);hrml.ParseLines();hrml.ParseQueries();return0;}voidHRML::ParseLines(){std::cin.ignore();for(inti=0;i<noOfLines;i++){stringline;std::getline(std::cin,line);if(line.find(openAnglebracket)!=string::npos&&line.find(closeTag)==string::npos){size_tposAfterTagName=ParseOpenAnglebracket(line);if(posAfterTagName!=0)UpdateTagAttributes(GetTagName(),line.substr(posAfterTagName));}elseif(line.find(closeTag)!=string::npos){if(!tagStack.empty())tagStack.pop();}}}size_tHRML::ParseOpenAnglebracket(stringline){size_tendPos=line.find_first_of(" ");if(endPos==string::npos)endPos=line.find(closeAnglebracket);if(endPos==string::npos)return0;stringtagName=line.substr(line.find(openAnglebracket)+1,endPos-1);UpdateTagStack(tagName);returnendPos;}voidHRML::UpdateTagStack(stringtagName){if(tagStack.empty()){tagStack.push(tagName);}else{stringnewTagName=tagStack.top()+"."+tagName;tagStack.push(newTagName);}}conststringHRML::GetTagName(){if(!tagStack.empty()){returntagStack.top();}else{return"";}}// trim from end of string (right)inlinestd::string&rtrim(std::string&s,constchar*t=" \t\n\r\f\v"){s.erase(s.find_last_not_of(t)+1);returns;}// trim from beginning of string (left)inlinestd::string<rim(std::string&s,constchar*t=" \t\n\r\f\v"){s.erase(0,s.find_first_not_of(t));returns;}stringHRML::Trim(stringstr,constchar*t){returnltrim(rtrim(str,t),t);}vector<string>HRML::ParseTagAttributes(stringstringAfterTagname){vector<string>attrVector;stringdelimiter="\"";// since only starting rags can have attributes, //below logic is not required considering attributes in mutilines/* while(stringAfterTagname.find(closeAnglebracket) == string::npos){ attrLines.push_back(stringAfterTagname); stringAfterTagname.clear(); getline(cin, stringAfterTagname); } attrLines.push_back(stringAfterTagname); */size_tbegin=0;stringattrString=stringAfterTagname.substr(0,stringAfterTagname.find(closeAnglebracket));while(size_tpos=attrString.find(delimiter)!=string::npos){size_tposSecondDoubleQuote=attrString.find(delimiter,pos+1);size_tposThirdDoubleQuote=attrString.find(delimiter,posSecondDoubleQuote+1);stringattrNameVal=Trim(attrString.substr(begin,posThirdDoubleQuote));attrVector.push_back(attrNameVal);attrString.erase(begin,posThirdDoubleQuote+delimiter.length());// begin = posThirdDoubleQuote + delimiter.length();}returnattrVector;}pair<string,string>HRML::GetAttributeNameValuePair(stringattrString){stringdelimiter="=";stringdoubleQuote="\"";size_tpos=attrString.find(delimiter);if(pos!=string::npos){size_tposDoubleQuote=attrString.find(doubleQuote);returnmake_pair(attrString.substr(0,pos-1),attrString.substr(posDoubleQuote+1));}else{returnmake_pair("","");}}voidHRML::UpdateTagAttributes(stringtagName,stringstringAfterTagname){//PrintTagStack();if(!tagName.empty()){vector<pair<string,string>>vectorAttributeNameValue;vector<string>attrVector=ParseTagAttributes(stringAfterTagname);for(stringattrString:attrVector){pair<string,string>attributeNameValuePair=GetAttributeNameValuePair(attrString);if(!attributeNameValuePair.first.empty()&&!attributeNameValuePair.second.empty())vectorAttributeNameValue.push_back(attributeNameValuePair);}if(!vectorAttributeNameValue.empty())mapTagNameAttribute[tagName]=vectorAttributeNameValue;}}voidHRML::ParseQueries(){for(inti=0;i<noOfQueries;i++){stringquery;getline(cin,query);stringvalue;size_tpos=query.find("~");stringtagName=query.substr(0,pos);stringattrName=query.substr(pos+1);vector<pair<string,string>>attrVector=mapTagNameAttribute[tagName];boolisFound=false;for(autoattr:attrVector){if(attr.first==attrName){cout<<attr.second<<endl;isFound=true;break;}}if(!isFound){cout<<"Not Found!"<<endl;}}}
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 →