Tuesday, 13 September 2016

Hacker Rank solution for Attribute Parser

Problem:- Hacker Rank Solution Program In C++ For " Attribute Parser " or Hacker Rank Solution Program In C++ For Attribute Parser or Attribute Parser solution hacker rank or Hacker rank solution for c++ domain or Hacker rank solution for Attribute Parser sub domain or Attribute Parser solution in c++ of hacker rank.

Check This- Hacker rank Solution for Sub Domain Introduction, Classes, STL, Inheritance.

Logic:- 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. we can take an example and try to find the solution step by step.

Example:- 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>

Also check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.

Now first step to take a 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 hacker rank.

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);
}

If you don't know the how push_back works I recommended see the below post then try.

Recommendation:- Hacker rank solution for STL

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 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 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

Output:-

Pratap
Ghanendra
Not Found!


Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. Before pasting the code into the editor make sure you have chosen c++ editor in the top right drop down option.

Submit your solution here:- Click here


Tip:- Before copy the program I recommended please read this full article, this will help you to build your own logic. 


If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about a new post.


Solution:-

#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 

Output:-

Hacker Rank solution for Attribute Parser

Hacker Rank solution for Attribute Parser


You May Like This:-












No comments:
Write comments

Recommended Posts × +