Attribute Parser

Sort by

recency

|

488 Discussions

|

  • + 0 comments

    That structured logic reminds me of how data needs clear hierarchy and mapping, much like how Charter Flights for Pet Travel UK ensures every step of the journey is properly linked and organized for smooth coordination.

  • + 0 comments

    The structured tagging and attribute referencing here really highlight how Free SEO Utility Tools That Deliver Results can help decode and organize layered data efficiently.

  • + 0 comments

    int main() { unordered_map> map;

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    //number of lines and number of querries
    int N, Q;
    smatch m;
    regex r_tag(R"(<(\w+))");
    regex r_tag_end(R"(</(\w+)>)");
    regex r_att(R"((\w+) = \"([^"]+)\")");
    regex r_q(R"((.+)~(\w+))");
    string lineage = ""; 
    cin >> N >> Q;
    for(auto i = 0; i <= N; ++i){
        string line; 
        getline(cin, line);
        //cout<<i<<"\n";
        //cout<<line<<"\n"; 
        regex_search(line, m, r_tag);
        if (m[1].matched){
            if (lineage.size()==0){
                lineage += string(m[1]);
            }
            else{
                lineage+="."+string(m[1]); 
            }
            unordered_map<string, string> att;
            //cout<<lineage<<"\n";
    
            auto begin = sregex_iterator(line.begin(), line.end(), r_att);
            auto end = sregex_iterator();
            for (auto it = begin; it != end; ++it) {
                smatch match = *it;
                //cout << "      att: " << match[1] << "=" << match[2] << "\n";
                att[string(match[1])] = string(match[2]);
            }
            map[lineage] = att;
        }
        regex_search(line, m, r_tag_end);
        if (m[1].matched){
            //cout<<line<<"\n";
            string rep = "\\.?"+string(m[1]);
            //string rep = "R("+string(m[1])+"\")";
            //cout<<rep<<"\n";
            regex rep_pat(rep);
            //cout<<"Lineage before "<<lineage<<"\n";
            string nlineage = regex_replace(lineage,rep_pat , "");
            lineage = nlineage;
            //cout<<lineage<<"\n";
        }
    }
    for(auto i = 0; i < Q; ++i){
        string query; 
        getline(cin, query);
        //cout<<"query: "<< query << "\n";
        regex_search(query, m, r_q);
        //cout << "Found  " << m[1] << "=" << m[2] << "\n";
        if(m[1].matched){
            string key = string(m[1]);
            //cout<<"key  "<<key<<"\n";
            if(map.find(key) != map.end()){
                string key1 = string(m[2]);
                //cout<<"key  "<<key<<"\n";
                //cout<<"key1 "<<key1<<"\n";
                if (map[key].find(key1) != map[key].end()){
                    cout<<map[key][key1]<<"\n";
                }else{
                    cout<<"Not Found!\n";
                }
            }else{
                cout<<"Not Found!\n";
            }
        }else{
            cout<<"Not Found!\n";
        }
    }
    return 0;
    

    }

  • + 0 comments

    `

    include

    include

    include

    include

    include

    include

    include

    include

    using namespace std;

    // The vector will have each tag we're nested inside. // so we'll iterate through that, then add the attribute onto the end // of the string to make the unique entry. string MakeEntry(vector& context, string& attribute) { string entry; for(auto& tag : context) { entry += tag; entry.push_back('.'); }

    // clear the last .
    entry.pop_back();
    entry.push_back('~');
    entry += attribute;
    
    return entry;
    

    }

    int main() { // fetch our data entry caps int lineNo, queryNo; cin >> lineNo >> queryNo; cin.get(); // clear

    // the map stores all of our entries 
    // e.g. { ("tag1.tag2~name", "name") }
    unordered_map<string, string> entries;
    vector<string> currentContext;
    
    for(int inputLine = 0; inputLine < lineNo; inputLine++)
    {
        // grab the whole line, then yeet the opening and closing brackets
        string line;
        getline(cin, line);
        line.pop_back();
        line = line.substr(1);
    
        // if the first character is / go back up a tag and
        // ignore the rest of the line, we don't need it.
        if(line[0] == '/')
        {
            currentContext.pop_back();
            continue;
        }
    
        // using a string stream here so we can loop through it.
        stringstream iss(line);
    
        // the first chunk will always be a new tag, so add it to our context
        string newContext;
        iss >> newContext;
        currentContext.push_back(newContext);
    
        string nextToken, attribute, value;
        while(iss >> nextToken)
        {
            // search for an equal incase it's embedded inside this chunk
            // e.g. foo=bar
            size_t equalPos = nextToken.find('=');
            if(equalPos != string::npos && nextToken.size() > 1)
            {
                // this chunk has an equal in it somewhere, so
                // it must be an attribute/value pair
                attribute = nextToken.substr(0, equalPos);
                value = nextToken.substr(equalPos+2, nextToken.size()-2);
    
                // add the pair to the map.
                string entry = MakeEntry(currentContext, attribute);
                entries.insert(make_pair(entry, value));
                attribute = ""; value ="";
            } else {
    
                // if the = is at the start or the size is < 1 skip!
                if(nextToken[0] == '=')
                {
                    continue;
                }
                // there's no = sign in this chunk...
                if(attribute == "")
                {
                    // we don't have a cached entry from a previous chunk
                    // so this is a new attribute.
                    attribute = nextToken;
                } else {
                    // otherwise, we now have a pair!
                    string entry = MakeEntry(currentContext, attribute);
    
                    // bit jank here, should ideally strip unwanted characters
                    // but this works.
                    entries.insert(make_pair(entry, nextToken.substr(equalPos+2, nextToken.size()-2)));
    
                    // clear for the next line so we don't double up.
                    attribute = ""; value = "";
                }
            }
        }
    }
    
     for(int query = 0; query < queryNo; query++)
     {
        string line;
        cin >> line;
    
        // we're literally just yeeting the whole line into the map to see if anything pops out.
        unordered_map<string, string>::const_iterator entry = entries.find(line);
        cout << (entry == entries.end() ? "Not Found!" : entry->second) << endl;
     }
         `
    

    test
    return 0; }

    `

  • + 0 comments
    #include <iostream>
    #include <vector>
    #include <string>
    #include <map>
    #include <sstream>
    #include <iomanip>
    
    using namespace std;
    
    class HRMLTree
    {
    private:
        HRMLTree *m_parent = nullptr;
        string m_tag;
        map<string, string> m_attributes;
        vector<HRMLTree *> m_childs;
    
    public:
        void setTag(string tag)
        {
            m_tag = tag;
        }
        void setParent(HRMLTree *parent)
        {
            m_parent = parent;
        }
        string getTag()
        {
            return m_tag;
        }
        const vector<HRMLTree *> &getChilds()
        {
            return m_childs;
        }
        HRMLTree *getParent()
        {
            return m_parent;
        }
        void addChild(HRMLTree *tree)
        {
            m_childs.push_back(tree);
        }
        string findAttribute(const string &attribute_name)
        {
            map<string, string>::iterator it;
            if ((it = m_attributes.find(attribute_name)) == m_attributes.end())
                return "Not Found!";
    
            return it->second;
        }
        void parseHRMLTagLine(string tag_line)
        {
            tag_line = tag_line.substr(1, tag_line.size() - 2);
            string attributes = parseTag(tag_line);
            parseAttributes2(attributes);
        }
        string parseTag(const string &tag_line)
        {
            int end = tag_line.find(' ');
            if (end == string::npos)
                end = tag_line.size();
    
            string tag = tag_line.substr(0, end);
            setTag(tag);
    
            return tag_line.substr(end);
        }
        void parseAttributes2(const string &attributes)
        {
            istringstream iss(attributes);
            string attribute_name, delimiter, attribute_value;
    
            while (iss >> attribute_name >> delimiter >> quoted(attribute_value))
            {
                m_attributes[attribute_name] = attribute_value;
            }
        }
        ~HRMLTree()
        {
            for (HRMLTree *child : m_childs)
                delete child;
    
            m_childs.clear();
        }
    };
    
    string readLine()
    {
        string line;
        line.reserve(200);
        getline(cin, line);
        return line;
    }
    
    void readHRMLTree(HRMLTree *parent, uint16_t tag_lines_count)
    {
        while (tag_lines_count > 0)
        {
            string tag_line = readLine();
    
            if (tag_line[1] == '/') // is ending tag?
            {
                parent = parent->getParent();
            }
            else
            {
                HRMLTree *child = new HRMLTree;
                child->parseHRMLTagLine(tag_line);
                child->setParent(parent);
                parent->addChild(child);
                parent = child;
            }
            tag_lines_count--;
        }
    }
    
    HRMLTree *findChild(HRMLTree *parent, const string &tag)
    {
        for (HRMLTree *child : parent->getChilds())
        {
            if (child->getTag() == tag)
                return child;
        }
    
        return nullptr;
    }
    
    void printQueries(HRMLTree *parent, uint16_t query_count)
    {
        while (query_count > 0)
        {
            string query = readLine();
    
            // split attribute name and tags
            int tilde_index = query.find('~');
            string attribute_name = query.substr(tilde_index + 1);
            string tags = query.substr(0, tilde_index);
    
            // loop over tags
            HRMLTree *parent_ref = parent;
            istringstream iss(tags);
            string tag;
    
            while (getline(iss, tag, '.'))
            {
                parent_ref = findChild(parent_ref, tag);
                if (!parent_ref)
                    break;
            }
    
            // print values
            if (!parent_ref)
                cout << "Not Found!" << endl;
            else
                cout << parent_ref->findAttribute(attribute_name) << endl;
    
            query_count--;
        }
    }
    
    int main()
    {
        uint16_t tag_lines_count, query_count;
        cin >> tag_lines_count >> query_count;
        cin.ignore();
    
        HRMLTree *tree = new HRMLTree;
        readHRMLTree(tree, tag_lines_count);
        printQueries(tree, query_count);
    
        delete tree;
    }