Attribute Parser in C++ hackerrank solution

Problem statement:

Given a set of N HTML tags with their attributes, followed by M queries, you need to print the value of the requested attribute for each query. If the attribute does not exist, print "Not Found!".

      
  

Sample Input:

The first line contains two space-separated integers, N (the number of tags) and M (the number of queries). The next N lines contain the HTML tags in the format . The following M lines contain the queries in the format , where X is the number of tags in the query. 4 3 <tag1 value = "HelloWorld"> <tag2 name = "Name1"> </tag2> </tag1> tag1.tag2.name tag1.tag2.value tag1.value
      

Sample Output:

Print the value of the requested attribute for each query on a new line. If the attribute is not found, print "Not Found!". Name1 Not Found! HelloWorld

Explanation:

In this example, we have N = 4 tags and M = 3 queries.

The first tag is .

The second tag is .

The third tag is
.

The fourth tag is
.

For the first query (tag1.tag2.name), we need to print the value of the attribute name within the nested tags and . The value is "Name1", so we print it as the output.

For the second query (tag1.tag2.value), we need to print the value of the attribute value within the nested tags and . However, the attribute is not found, so we print "Not Found!" as the output.

For the third query (tag1.value), we need to print the value of the attribute value within the tag . The value is "HelloWorld", so we print it as the output.

      
  #include <iostream>
  #include <map>
  #include <vector>
  #include <sstream>
  
  using namespace std;
  
  int main() {
      int n, m;
      cin >> n >> m;
      cin.ignore();  // Ignore the newline character after reading n and m
  
      map tagAttributes;  // Map to store tag attributes
      string currentTag;  // Current tag being processed
  
      // Read the HTML tags and their attributes
      for (int i = 0; i < n; i++) {
          string line;
          getline(cin, line);
  
          // Check if the tag is an opening tag
          if (line[1] != '/') {
              stringstream ss(line);
              string tag, attribute, equals, value;
              ss >> tag >> attribute >> equals >> value;
              tag = tag.substr(1);  // Remove the opening angle bracket from the tag
  
              // Check if the tag has attributes
              if (attribute != "") {
                  attribute = attribute.substr(0, attribute.length() - 1);  // Remove the trailing equals sign
                  tagAttributes[currentTag + '.' + tag + '~' + attribute] = value.substr(1, value.length() - 2);  // Store the attribute value
              }
  
              currentTag += '.' + tag;  // Update the current tag
          }
          else {
              // Check if the tag is a closing tag
              currentTag = currentTag.substr(0, currentTag.rfind('.'));
          }
      }
  
      // Process the queries
      for (int i = 0; i < m; i++) {
          string query;
          cin >> query;
  
          // Check if the query exists in the tagAttributes map
          if (tagAttributes.find(query) != tagAttributes.end()) {
              cout << tagAttributes[query] << endl;
          }
          else {
              cout << "Not Found!" << endl;
          }
      }
  
      return 0;
  }
  

Code Explanation


We start by reading the values of N and M, which represent the number of tags and queries, respectively.

We use cin.ignore() to ignore the newline character after reading N and M.

We define a map called tagAttributes to store the tag attributes.

We define a string variable currentTag to keep track of the current tag being processed. This will help in constructing the complete attribute query string.

We iterate N times and read the HTML tags and their attributes using getline(cin, line).

If the tag is an opening tag (i.e., it doesn't start with '
If the tag has attributes, we store the attribute value in the tagAttributes map using the complete attribute query string as the key.

We update the currentTag by appending the current tag name.

If the tag is a closing tag (i.e., it starts with '
After reading all the tags, we iterate M times to process the queries.

For each query, we check if it exists in the tagAttributes map.

If the query exists, we print the corresponding attribute value. Otherwise, we print "Not Found!".

Finally, we return 0 to indicate successful program execution.

This solution uses a map to store the tag attributes, allowing for efficient retrieval based on the attribute query string. It processes the input tags and queries in the order they are provided and prints the attribute values or "Not Found!" as per the requirements.

I hope this solution and explanation are helpful!