HackerRank C++ Attribute Parser




We have defined our own markup language HRML. In HRML, each element consists of a starting and ending tag, and there are attributes associated with each tag. Only starting tags can have attributes. We can call an attribute by referencing the tag, followed by a tilde, '~' and the name of the attribute. The tags may also be nested.

The opening tags follow the format:

The closing tags follow the format:

For example:





The attributes are referenced as:

tag1~value
tag1.tag2~name
You are given the source code in HRML format consisting of N lines. You have to answer Q queries. Each query asks you to print the value of the attribute specified. Print "Not Found!" if there isn't any such attribute.



  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n, q,i;
  6. cin>>n>>q;
  7. string temp;
  8. vector<string> hrml;
  9. vector<string> quer;
  10. cin.ignore();
  11. for(i=0;i<n;i++)
  12. {
  13. getline(cin,temp);
  14. hrml.push_back(temp);
  15. }
  16. for(i=0;i<q;i++)
  17. {
  18. getline(cin,temp);
  19. quer.push_back(temp);
  20. }
  21. map<string, string> m;
  22. vector<string> tag;
  23. for(i=0;i<n;i++)
  24. {
  25. temp=hrml[i];
  26. temp.erase(remove(temp.begin(), temp.end(), '\"' ),temp.end());
  27. temp.erase(remove(temp.begin(), temp.end(), '>' ),temp.end());
  28. if(temp.substr(0,2)=="</")
  29. {
  30. tag.pop_back();
  31. }
  32. else
  33. {
  34. stringstream ss;
  35. ss.str("");
  36. ss<<temp;
  37. string t1,p1,v1;
  38. char ch;
  39. ss>>ch>>t1>>p1>>ch>>v1;
  40. string temp1="";
  41. if(tag.size()>0)
  42. {
  43. temp1=*tag.rbegin();
  44. temp1=temp1+"."+t1;
  45. }
  46. else
  47. temp1=t1;
  48. tag.push_back(temp1);
  49. m[*tag.rbegin()+"~"+p1]=v1;
  50. while(ss)
  51. {
  52. ss>>p1>>ch>>v1;
  53. m[*tag.rbegin()+"~"+p1]=v1;
  54. }
  55. }
  56. }
  57. for(i=0;i<q;i++){
  58. if (m.find(quer[i]) == m.end())
  59. cout << "Not Found!\n";
  60. else
  61. cout<<m[quer[i]]<<endl;
  62. }
  63. return 0;
  64. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube