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.
- Prepare
- C++
- Strings
- Attribute Parser
- Discussions
Attribute Parser
Attribute Parser
+ 0 comments Here are the solution of Attribute Parser in C++ Hacker Rank Solution
+ 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; }
+ 0 comments Here is problem solution - https://thecscience.com/hackerrank-attribute-parser-in-cpp-problem-solution.html
+ 0 comments Here is the Solution of Attribute Parser in C++ HackerRank Solution https://www.brokenprogrammers.com/attribute-parser-in-cpp-hackerrank-solution/
Load more conversations
Sort 389 Discussions, By:
Please Login in order to post a comment