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>usingnamespacestd;vector<string>getQuery(vector<string>query){intl,st;stringbuf;query.clear();getline(cin,buf);l=buf.length();st=0;for(inti=0;i<l;i++){if(buf[i]=='.'){query.push_back(buf.substr(st,i-st));st=i+1;}elseif(buf[i]=='~'){query.push_back(buf.substr(st,i-st));st=i;query.push_back(buf.substr(st,l-st));}}returnquery;}voidresolveQuery(vector<string>query,vector<string>listing){intlQ=query.size();intlL=listing.size();intiQ=0;intlistS,attL,lineL;vector<string>currentTagList;for(intj=0;j<lL;j++){if(listing[j][1]=='/'){currentTagList.pop_back();//we remove last tag from the list, valid listing means, we dont need to check what tag it is}else{intk=1;while(listing[j][k]!=' '&&listing[j][k]!='>'){k++;}stringnewTag=listing[j].substr(1,k-1);currentTagList.push_back(newTag);if(newTag==query[iQ]){iQ++;if(query[iQ][0]=='~'){//we are on the attribute query, our value is on this line, or nowherelistS=currentTagList.size();for(inti=0;i<listS;i++){if(currentTagList[i]!=query[i]){cout<<"Not Found!"<<endl;return;}}attL=query[iQ].length()-1;//don't count '~'lineL=listing[j].length();for(inti=1;i+attL<lineL;i++){if(listing[j].substr(i,attL+1)==' '+query[iQ].substr(1,attL)){k=i+attL;while(listing[j][k]!='"'){k++;}k++;while(listing[j][k]!='"'){cout<<listing[j][k];k++;}cout<<endl;return;}}cout<<"Not Found!"<<endl;//since we didn't find <attribute> on this line we can safetly returnreturn;}}}}cout<<"Not Found!"<<endl;//if we gone through the whole list without finding out tag we do not forget to print the resultreturn;}intmain(){vector<string>listing;vector<string>query;intt,q;stringbuffer;cin>>t>>q;listing.reserve(t);getline(cin,buffer);//clearing cin from last endlfor(inti=0;i<t;i++){getline(cin,buffer);listing.push_back(buffer);}for(inti=0;i<q;i++){query=getQuery(query);resolveQuery(query,listing);}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 →