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 haven't touched C++ in a while and was trying to get some of the syntax back, my copy's and \0 etc are definitely not the right way to go all that could be done in 1 line.
I use "|" to differentiate "tags" from "attributes"
#include<cstdio>#include<vector>#include<iostream>#include<sstream>usingnamespacestd;intsearch_for_tag(size_tstart,strings_end,vector<string>&vec,stringneedle){intcurr=start;intend=vec.size();stringendcur;charbuff[200];while(true){if(vec[curr][0]=='|'){//see if its the oneif(!vec[curr].compare(needle)){returncurr;//see if its the end}elseif(!vec[curr].compare(s_end)){return-1;//pass this tag}else{vec[curr].copy(buff,vec[curr].size(),0);buff[vec[curr].size()]='\0';endcur="|"+string(buff);endcur.replace(1,1,"/");while(vec[curr].compare(endcur)){curr++;}curr++;if(curr==end)return-1;}}else{while(vec[curr][0]!='|'){curr++;}}}}intsearch_for_att(size_tstart,strings_end,vector<string>&vec,stringneedle){intcurr=start;while(vec[curr][0]!='|'){if(!vec[curr].compare(needle)){returncurr;}curr++;if(curr==vec.size())return-1;}return-1;}intmain(){inttlines,qlines;stringints;getline(cin,ints);stringstreamss(ints);ss>>tlines;ss>>qlines;stringline;charbuf[200];size_tdel_tag;vector<string>contain;//Parse the shenanigansfor(inti=0;i<tlines;i++){getline(cin,line);del_tag=line.find_first_of(" >");//Get Tagline.copy(buf,del_tag-1,1);buf[del_tag-1]='\0';stringtagbuf(buf);tagbuf="|"+tagbuf;contain.push_back(tagbuf);size_tfound=line.find("=",del_tag);while(found!=string::npos){//Get the attributeline.copy(buf,found-del_tag-2,del_tag+1);buf[found-del_tag-2]='\0';stringattbuf(buf);contain.push_back(attbuf);//move past the =del_tag=found+2;found=line.find("\"",del_tag+1);//Copy the valueline.copy(buf,found-del_tag-1,del_tag+1);buf[found-del_tag-1]='\0';stringvalbuf(buf);contain.push_back(valbuf);del_tag=line.find(" ",found);found=line.find("=",found);}}//Query Time for(inti=0;i<qlines;i++){size_tstartString=0;booltypeTag=true;size_tstartVec=0;stringendVec="";boolnotFound=true;stringattval;stringline;getline(cin,line);while(true){//Get next tokensize_tfound=line.find_first_of(".~",startString);if(found==string::npos)found=line.size();line.copy(buf,found-startString,startString);buf[found-startString]='\0';stringtagbuf(buf);//Check if its a Tagif(startString>0&&line[startString-1]=='~'){typeTag=false;}else{tagbuf="|"+tagbuf;}//Find a tagif(typeTag){intres=search_for_tag(startVec,endVec,contain,tagbuf);if(res<0){break;}else{startVec=res+1;endVec="|"+tagbuf;endVec.replace(1,1,"/");startString=found+1;}}else{//Find an attibuteintres=search_for_att(startVec,endVec,contain,tagbuf);if(res>=0){attval=contain[res+1];notFound=false;}break;}}//Printingif(notFound){cout<<"Not Found!"<<endl;}else{cout<<attval<<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 →
Nice, almost as ugly as mine :P
I haven't touched C++ in a while and was trying to get some of the syntax back, my copy's and \0 etc are definitely not the right way to go all that could be done in 1 line.
I use "|" to differentiate "tags" from "attributes"