We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. C++
  3. Strings
  4. Attribute Parser
  5. Discussions

Attribute Parser

Problem
Submissions
Leaderboard
Discussions

Sort 389 Discussions, By:

recency

Please Login in order to post a comment

  • gangadharghru67
    2 weeks ago+ 0 comments

    Here are the solution of Attribute Parser in C++ Hacker Rank Solution

    0|
    Permalink
  • gcysne
    3 weeks ago+ 0 comments

    I used a stack to build the valid paths and a hash-map to store the attributes values for every valid path found.

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <list>
    #include <map>
    #include <sstream>
    using namespace std;
    
    int main() {    
        int N,Q;
        std::map<string, string> valid_paths;
        
        cin >> N >> Q;
        // Build the valid paths while processing
        string tag;
        list<string> current_path;
        std::getline(std::cin, tag);
        while(N--){
            std::getline(std::cin, tag);                
            // If it's a openning tag, append the name to the 
            // current path with the delimiter char '.' and
            // process the attributes
            if(tag[1] != '/'){
                if(!current_path.empty()){
                    current_path.push_back(".");    
                }            
                std::istringstream iss(tag);
                std::string token;
                while (std::getline(iss, token, ' ')){
                    if(token[0] == '<'){                    
                        // It's the tag name
                        current_path.push_back(token.substr(1,token.find_last_of('>')-1));                    
                    }else{
                        // It's a attribute name
                        // Flatten the current path and add it to the map
                        // of the valid paths
                        std::string atrr_name = token;
                        std::string atrr_value;
                        std::string valid_path;
                        for(string s : current_path){
                            valid_path += s;
                        }
                        std::getline(iss, token, ' '); // Get the '='
                        std::getline(iss, token, ' '); // Get the attr-value
                        atrr_value = token.substr(1,token.find_last_of('"')-1);
                        
                        valid_path += "~"+atrr_name;                    
                        valid_paths[valid_path] = atrr_value;
                    }
                }            
            // If it's a closing tag, remove the tag name
            // from the current path (and the '.' if necessary) 
            }else{
                current_path.pop_back();
                if(!current_path.empty()){
                    current_path.pop_back();  
                }          
            }        
        }
        
        while(Q--){
            string query;
            cin >> query;
            
            string query_result = valid_paths[query];
            if(query_result.empty()){
                query_result = "Not Found!";
            }
            cout << query_result << endl;
        }
        
        return 0;
    }
    
    1|
    Permalink
  • poonamdeorasurp1
    4 weeks ago+ 0 comments

    Here is problem solution - https://thecscience.com/hackerrank-attribute-parser-in-cpp-problem-solution.html

    0|
    Permalink
  • brokenprogramme1
    1 month ago+ 0 comments

    Here is the Solution of Attribute Parser in C++ HackerRank Solution https://www.brokenprogrammers.com/attribute-parser-in-cpp-hackerrank-solution/

    0|
    Permalink
  • aj790265
    1 month ago+ 0 comments

    The quintessence of many brandishing vocations is lifting their game's most renowned prize for their nation or club or person. To numerous competitors, these prizes and a big motivator for they are precious.

    0|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy