Attribute Parser

Sort by

recency

|

467 Discussions

|

  • + 0 comments

    Awesome! This post is very informative and effective. I found exactly what I was looking for. Thank you!

  • + 0 comments

    Wonderful post! It’s engaging and full of valuable information. Thank you for sharing!

  • + 0 comments

    One important key is the tag name is actually not "tagxxx", it can be random stuff "a" like <abc attr1 = "adt1"> . Was kind of confused by example then realize from one of failed test case.

    Following are my approach

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <stack>
    #include <sstream>
    using namespace std;
    
    
    int main() {
        map<string, map<string, string>> tags;
        
        stack<string> currentTags;
        
        string input;
        getline(cin, input);
        stringstream initSS(input);
        int line{};
        int query{};
        initSS >> line >> query;
        
        string fullPath = "";
       
        for(int i = 0; i < line; i++) {
            getline(cin, input);
    
            // remove the tag
            input = input.substr(1, input.length() - 2);
                    
            stringstream ss(input);
            
            // work on tag
            string tag;
            ss >> tag;
            
            if(tag[0] == '/') {
                currentTags.pop();
                
                if(currentTags.size() > 0) {
                    fullPath = currentTags.top();
                }
                else {
                    fullPath.clear();
                }
                
                // skip to next line
                continue;
            }
            
        
                    if(currentTags.size() > 0) {
                        // add "." if already has path
                        fullPath.append(".");
                    }
                    fullPath.append(tag);
                    
                    // if no previous tag
                    currentTags.push(fullPath);
                    
                    if(tags.find(fullPath) == tags.end()) {
                        tags[fullPath] = map<string, string>();
                    }
       
                
            // work on attribute, get all remain as one single string
            string attr, eq, value;
            
            while(ss >> attr >> eq >> value) {
                if(value[0] == '"') {
                    value = value.substr(1, value.size() - 2);
                }
    
                      
                tags[fullPath][attr] = value;
            
            }
            
        }
        
        for(int i = 0; i < query; i++) {
            getline(cin, input);
                
                int index = input.find('~');
                string tag = input.substr(0, index);
                string query = input.substr(index + 1, input.size());
                        // indicate not found
                        if(tags.count(tag) && 
                        tags[tag].count(query)) {
                                    cout << tags[tag][query] << "\n";  
                            }
                        else {
                        cout << "Not Found!\n";
                        }
            
        }
        
        return 0;
    }
    
  • + 0 comments

    Hackerrank do you even test your own stuff????

    this is the input you pritend to send:

    <tag1 value = "HelloWorld">
    

    and this is what the app actually gets (NO SPACES in the string):

    <tag1value="HelloWorld">
    

    a bunch of a***hoses

  • + 0 comments

    What should i focus on "Topic" in order to find the solution of this