Tuesday, 13 September 2016

Hacker Rank solution for Attribute Parser In C++

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.


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>

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.

Submit your solution here:- Click here



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












Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment