9/13/2016

Attribute Parser Hackerrank Solution in C++ | 44 Solutions

Get all 44 solutions of Attribute Parser Hackerrank Solution in C++| Hackerrank C++ domain. We can take an example and try to find the solution of 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 language. Below you can find the example and logic of Attribute Parser in C++.

Attribute Parser Hackerrank Solution in C++

Attribute Parser Hackerrank Solution in C++ Logic


The logic is very simple to 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 a data from beginning and end with the end of the string. and in the last run the string for find the result in the Attribute Parser. if the tag is a match then it will print the attribute value.

Hackerrank Attribute Parser C++ Example


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

The opening tags follow the format:


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

The closing tags follow the format:

< /tag-name >

For example:

<tag1 value = "HelloWorld">
<tag2 name = "Name1">
</tag2>
</tag1>

Now first step to take user input and put the user input into the vector string array, first we will take an input N(denotes the number of lines) and Q(denotes the number of queries). Then allow to user take the input in the appropriate manner according to hackerrank.

cin>>n>>q;
string temp;
vector<string> hrml;
vector<string> quer;
cin.ignore(); // used for clear the buffer while taking next line input.
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);
}
 
Finally, we have found a solution to Hackerrank Attribute Parser C++. If you don't know the how push_back works I recommended see the below post then try.

Recommendation: Hackerrank 30 Days of Code Solutions in C Language

Now erase the irreverent tags from a string by as shown in given below, first put a string character by character in temp until the closing tag.

Erasing Tags

temp=hrml[i];
temp.erase(remove(temp.begin(), temp.end(), '\"' ),temp.end());
temp.erase(remove(temp.begin(), temp.end(), '>' ),temp.end());
 
Now come to the query section if the query belongs to the same tag then use a ~ symbol and if you want to access the tag2 value with tag1 then use a. (dot) for finding the value in tag2. Below is some query which consists of all types of a query for the problem.

Eg:-

4 3 // number of lines and number of query

<tag1 value = "Ghanendra">
<tag2 name = "Pratap">
</tag2>
</tag1>
 
// Query begins

tag1.tag2~name
tag1~value
tag2~value

The Output of Hackerrank Attribute Parser C++


Pratap
Ghanendra
Not Found!

The Output of Hackerrank Attribute Parser C++

Submit your solution here:-Click here

Attribute Parser Hackerrank Solution in C++


Below is the Attribute Parser Hackerrank Solution C++ programming language in details. I am requesting to all of you please don't just copy and paste try to understand the code and before submitting a solution try at least once.

#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;
 
//Hint By Click Here Thanks for your hint to solve Hackerrank Attribute Parser C++.

Attribute Parser Hackerrank Solution C++ Output


Attribute Parser Hackerrank Solution C++ Output

Attribute Parser Hackerrank Solution Output
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

3 comments:

  1. Can u pls explain this : temp.erase(remove(temp.begin(), temp.end(), '\"' ),temp.end());

    ReplyDelete
  2. remove(temp.begin(), temp.end(), '\"' ): remove() will take out all the quotes(") from the string temp and move the end pointer to end of the new string, we'll call this end pointer- new end pointer.

    temp.erase():Then .erase() will erase the char between the new end pointer and old end pointer.

    Use this links as refrences:
    http://www.cplusplus.com/reference/string/string/erase/
    http://www.cplusplus.com/reference/algorithm/remove/

    ReplyDelete
    Replies
    1. Thank you for clarification. Please keep visit

      Delete