Example<\/strong><\/p>\n\n\n\nHRML listing\n<tag1 value = \"value\">\n<tag2 name = \"name\">\n<tag3 another=\"another\" final=\"final\">\n<\/tag3>\n<\/tag2>\n<\/tag1>\n\nQueries\ntag1~value\ntag1.tag2.tag3~name\ntag1.tag2~value<\/pre>\n\n\n\nHere, tag2 is nested within tag1, so attributes of tag2 are accessed as tag1.tag2~<attribute><\/code>. Results of the queries are:<\/p>\n\n\n\nQuery Value\ntag1~value \"value\"\ntag1.tag2.tag3~name \"Not Found!\"\ntag1.tag2.tag3~final \"final\"<\/pre>\n\n\n\nInput Format<\/strong><\/p>\n\n\n\nThe first line consists of two space separated integers, N<\/em><\/strong> and Q<\/em><\/strong>. N<\/em><\/strong> specifies the number of lines in the HRML source program. Q<\/em><\/strong> specifies the number of queries.<\/p>\n\n\n\nThe following N<\/em><\/strong> lines consist of either an opening tag with zero or more attributes or a closing tag. There is a space after the tag-name, attribute-name, ‘=’ and value.There is no space after the last value. If there are no attributes there is no space after tag name.<\/em><\/p>\n\n\n\nQ<\/em><\/strong> queries follow. Each query consists of string that references an attribute in the source program.More formally, each query is of the form tagi1. tagi2. tagi3 . . . tagim<\/em><\/strong> ~ attr – name<\/strong><\/em> where m >= 1<\/strong> and tagi1. tagi2. tagi3 . . . tagim<\/em><\/strong> are valid tags in the input.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= N<\/em> <= 20<\/strong><\/li>
- 1 <= Q<\/em> <= 20<\/strong><\/li>
- Each line in the source program contains, at most, 200<\/strong> characters.<\/li>
- Every reference to the attributes in the Q<\/em><\/strong> queries contains at most 200<\/strong> characters.<\/li>
- All tag names are unique and the HRML source program is logically correct, i.e. valid nesting.<\/li>
- A tag can may have no attributes.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint the value of the attribute for each query. Print “Not Found!<\/em>” without quotes if the attribute does not exist.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n4 3\n<tag1 value = \"HelloWorld\">\n<tag2 name = \"Name1\">\n<\/tag2>\n<\/tag1>\ntag1.tag2~name\ntag1~name\ntag1~value<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\nName1\nNot Found!\nHelloWorld<\/code><\/pre>\n\n\n\n<\/span>Solution – Attribute Parser in C++<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <bits\/stdc++.h>\nusing namespace std;\n\nint main()\n{\nint n, q,i;\ncin>>n>>q;\nstring temp;\nvector<string> hrml;\nvector<string> quer;\ncin.ignore();\n\nfor(i=0;i<n;i++)\n{\n getline(cin,temp);\n hrml.push_back(temp);\n}\nfor(i=0;i<q;i++)\n{\n getline(cin,temp);\n quer.push_back(temp);\n}\n\nmap<string, string> m;\nvector<string> tag;\n\nfor(i=0;i<n;i++)\n{\n temp=hrml[i];\n temp.erase(remove(temp.begin(), temp.end(), '\\\"' ),temp.end());\n temp.erase(remove(temp.begin(), temp.end(), '>' ),temp.end());\n\n if(temp.substr(0,2)==\"<\/\")\n {\n tag.pop_back();\n }\n else\n {\n stringstream ss;\n ss.str(\"\");\n ss<<temp;\n string t1,p1,v1;\n char ch;\n ss>>ch>>t1>>p1>>ch>>v1;\n string temp1=\"\";\n if(tag.size()>0)\n {\n temp1=*tag.rbegin();\n temp1=temp1+\".\"+t1;\n }\n else\n temp1=t1;\n tag.push_back(temp1);\n m[*tag.rbegin()+\"~\"+p1]=v1;\n while(ss)\n {\n ss>>p1>>ch>>v1;\n m[*tag.rbegin()+\"~\"+p1]=v1;\n }\n }\n\n}\n\nfor(i=0;i<q;i++){\n if (m.find(quer[i]) == m.end())\n cout << \"Not Found!\\n\";\n else\n cout<<m[quer[i]]<<endl;\n}\nreturn 0;\n\n}<\/pre>\n\n\n\n