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.
Considering so many complaints about problem being marked "Medium", I would like to say, in fact, that is a clue about the solution. Scrolling through discussions, we can find several lengthy and complicated solutions. It isn't supposed to be that way.
The first most important step towards solving any problem is to read the problem well, and use all the information provided in the description properly. Failing to do that, the result will be messy code. Following line from constraints gives a big hint :
All tag names are unique and the HRML source program is logically correct.
This means most of the hard-work had already been done. If we ignore this, we will be just writing needless code doing pointless computations. In stead, if we use the info properly, we'll surely get far elegant solution. Here's my approach for interested coders:
#include<iostream>#include<map>usingnamespacestd;map<string,string>tagMap;voidcreateMap(int&n,stringpretag){if(!n)return;stringline,tag,attr,value;getline(cin,line);inti=1;if(line[i]=='/'){// found closing tagwhile(line[i]!='>')i++;if(pretag.size()>(i-2))// update tagtag=pretag.substr(0,pretag.size()-i+1);elsetag="";}else{// found opening tagwhile(line[i]!=' '&&line[i]!='>')i++;tag=line.substr(1,i-1);// update tagif(pretag!="")tag=pretag+"."+tag;intj;while(line[i]!='>'){// go through attributesj=++i;while(line[i]!=' ')i++;attr=line.substr(j,i-j);// attribute namewhile(line[i]!='\"')i++;j=++i;while(line[i]!='\"')i++;value=line.substr(j,i-j);// attribute valuei+=1;tagMap[tag+"~"+attr]=value;}}createMap(--n,tag);}intmain(){intn,q;cin>>n>>q;cin.ignore();createMap(n,"");stringattr,value;while(q--){getline(cin,attr);value=tagMap[attr];if(value=="")value="Not Found!";cout<<value<<endl;}return0;}
Attribute Parser
You are viewing a single comment's thread. Return to all comments →
Considering so many complaints about problem being marked "Medium", I would like to say, in fact, that is a clue about the solution. Scrolling through discussions, we can find several lengthy and complicated solutions. It isn't supposed to be that way.
The first most important step towards solving any problem is to read the problem well, and use all the information provided in the description properly. Failing to do that, the result will be messy code. Following line from constraints gives a big hint :
This means most of the hard-work had already been done. If we ignore this, we will be just writing needless code doing pointless computations. In stead, if we use the info properly, we'll surely get far elegant solution. Here's my approach for interested coders:
Informative Tweets for Inquisitive Minds