19/03/2023

Attribute Parser Hackerrank Solution in C++ | Hackerrank C++

Get all 44 solutions of Attribute Parser Hackerrank Solution in C++. This challenge works with a custom-designed markup language HRM. Hackerrank C++ domain. We can take an example and try to find the solution of an Attribute Parser in C++ step by step. We have also provided all 44 questions solutions of hackerrank competitive website solution of C++ domain and subdomain. All the solutions are provided with an example, logic, and complete code with an output of code.

We provide to solve particular programming challenges in a different-different programming languages. Below you can find the example and logic of an Attribute Parser in C++.

Attribute Parser Hackerrank Solution in C++

This challenge works with a custom-designed 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:

<tag-name attribute1-name = "value1" attribute2-name = "value2" ...>

The closing tags follow the format:

</tag-name>

The attributes are referenced as:

tag1~value  
tag1.tag2~name

Given the source code in HRML format consisting of N lines, answer Q queries. For each query, print the value of the attribute specified. Print "Not Found!" if the attribute does not exist.

Example

HRML listing
<tag1 value = "value">
<tag2 name = "name">
<tag3 another="another" final="final">
</tag3>
</tag2>
</tag1>

Queries
tag1~value
tag1.tag2.tag3~name
tag1.tag2~value

Here, tag2 is nested within tag1, so attributes of tag2 are accessed as tag1.tag2~<attribute>. The results of the queries are:

Query                 Value
tag1~value            "value"
tag1.tag2.tag3~name   "Not Found!"
tag1.tag2.tag3~final  "final"

Input Format

The first line consists of two space-separated integers, N and Q. N specifies the number of lines in the HRML source program. Q specifies the number of queries.

The following N 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 the tag name.

Q queries follow. Each query consists of a string that references an attribute in the source program. More formally, each query is of the form.

Constraints

  • 1 <= N <= 20
  • 1 <= Q <= 20
  • Each line in the source program contains, at most, 200 characters.
  • Every reference to the attributes in the Q queries contains at most 200 characters.
  • All tag names are unique and the HRML source program is logically correct, i.e. valid nesting.
  • A tag can have no attributes.

Output Format

Print the value of the attribute for each query. Print "Not Found!" without quotes if the attribute does not exist.

Sample Input

4 3
<tag1 value = "HelloWorld">
<tag2 name = "Name1">
</tag2>
</tag1>
tag1.tag2~name
tag1~name
tag1~value

Sample Output

Name1
Not Found!
HelloWorld

Attribute Parser Hackerrank Solution in C++


#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n, q, i;
	cin >> n >> q;
	string temp;
	vector<string> hrml;
	vector<string> quer;
	cin.ignore();

	for (i = 0; i < n; i++)
	{
		getline(cin, temp);
		hrml.push_back(temp);
	}

	for (i = 0; i < q; i++)
	{
		getline(cin, temp);
		quer.push_back(temp);
	}

	map<string, string> m;
	vector<string> tag;

	for (i = 0; i < n; i++)
	{
		temp = hrml[i];
		temp.erase(remove(temp.begin(), temp.end(), '\"'), temp.end());
		temp.erase(remove(temp.begin(), temp.end(), '>'), temp.end());

		if (temp.substr(0, 2) == "</")
		{
			tag.pop_back();
		}
		else
		{
			stringstream ss;
			ss.str("");
			ss << temp;
			string t1, p1, v1;
			char ch;
			ss >> ch >> t1 >> p1 >> ch >> v1;
			string temp1 = "";
			if (tag.size() > 0)
			{
				temp1 = *tag.rbegin();
				temp1 = temp1 + "." + t1;
			}
			else
				temp1 = t1;
			tag.push_back(temp1);
			m[*tag.rbegin() + "~" + p1] = v1;
			while (ss)
			{
				ss >> p1 >> ch >> v1;
				m[*tag.rbegin() + "~" + p1] = v1;
			}
		}
	}

	for (i = 0; i < q; i++)
	{
		if (m.find(quer[i]) == m.end())
			cout << "Not Found!\n";
		else
			cout << m[quer[i]] << endl;
	}

	return 0;

}

The Output of Attribute Parser Hackerrank Solution


The Output of Attribute Parser Hackerrank Solution

Submit your solution here: Click here

Attribute Parser Logic


The logic is very simple find the Attribute Parser Hackerrank Solution in C++. So for Attribute Parser, we have to take user input in string type and then take a two-vector string type and push the string in both vectors. Now we map the string and erase irrelevant data from a string.

We remove or erase data from the beginning and end with the end of the string. and in the last run, the string finds the result in the Attribute Parser. if the tag is a match then it will print the attribute value.

Get the example of an Attribute Parser in C++. Before we start we have to understand the tags, so for this problem, we are taking hackerrank examples of tags. Below are some basic ideas about Tags, basically, tags are widely used in HTML language.

Similar to Attribute Parser


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: