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<map>#include<string>usingnamespacestd;// struct Node{// map<string,string> attr_map;// map<string,Node> childMap;// Node * parent = nullptr;// string tag_name;// };stringgetHash(constvector<string>&vec){stringhash="";for(autoc:vec){hash+=c+".";}returnhash;}intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */uint32_tN,Q;cin>>N>>Q;vector<string>stack;boollisting_flg=true;map<string,map<string,string>>tagMap;for(inti=0;i<N;i++){strings;cin>>s;// string hash_ = getHash(stack);// cout << "|" << hash_ <<"\n";// Determine Starting Tag or Ending Tagif(s.find("/")!=string::npos){// Go to the next line if this is a end tag// string tag_name = s.substr(2, s.length()-3);// auto tail = stack.back();stack.pop_back();}else{// Starting Tag. Scan attributesmap<string,string>attr_map;stringhash_prefix=getHash(stack);if(s.find(">")==string::npos){s=s.substr(s.find("<")+1);tagMap[hash_prefix+s]=attr_map;stack.push_back(s);}else{// no attribute for this tag, skip followingautostart_pos=s.find("<");autoend_pos=s.find(">");tagMap[hash_prefix+s.substr(start_pos+1,end_pos-start_pos-1)]=attr_map;stack.push_back(s.substr(start_pos+1,end_pos-start_pos-1));continue;}stringattr;cin>>attr;// ">" is end of the lineboolval_flg=false;stringattr_name;stringattr_value;while(attr.find(">")==string::npos){if(attr.compare("=")==0){attr.clear();cin>>attr;continue;}elseif(attr.find(">")!=string::npos){autodelimit_pos=attr.find("=");attr_name=attr.substr(0,delimit_pos-1);attr_value=attr.substr(delimit_pos+1,attr.length()-attr_name.length()-2);tagMap[hash_prefix+s][attr_name]=attr_value;val_flg=false;}else{if(val_flg){val_flg=false;attr_value=attr.substr(1,attr.length()-2);tagMap[hash_prefix+s][attr_name]=attr_value;}else{val_flg=true;attr_name=attr;}}attr.clear();cin>>attr;}// handle the last valueattr_value=attr.substr(1,attr.length()-3);tagMap[hash_prefix+s][attr_name]=attr_value;}}//// Read queryfor(inti=0;i<Q;i++){stringq;cin>>q;autotilde_pos=q.find("~");autohash_key=q.substr(0,tilde_pos);autoattr_name=q.substr(tilde_pos+1,q.length()-hash_key.length()-1);// cout << hash_key << "|" << attr_name << '\n';if(tagMap.find(hash_key)==tagMap.end()){cout<<"Not Found!"<<endl;}else{autoit=tagMap.find(hash_key);if(it->second.find(attr_name)==it->second.end()){cout<<"Not Found!"<<endl;}else{autoit2=it->second.find(attr_name);cout<<it2->second<<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 →
Got it worked,