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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. C++
  3. Strings
  4. Attribute Parser
  5. Discussions

Attribute Parser

Problem
Submissions
Leaderboard
Discussions

Sort 403 Discussions, By:

recency

Please Login in order to post a comment

  • yehiaazab336
    4 weeks ago+ 0 comments

    class Tag { public:

    vector< pair< string , string > > att;
    string title;
    string queryFormat;
    vector< Tag > children;
    bool closeTag;
    Tag(){}
    
    Tag(string s){
        s.erase(s.end() -1);
        s.erase(s.begin() ,  s.begin() + 1);   
        if (s[0] == '/')
        {
            closeTag = true;
            s.erase(s.begin() ,  s.begin() + 1);   
        }else{
            closeTag = false;
        }
    
        replace(s.begin() , s.end() , '=' , ' ');
        replace(s.begin() , s.end() , '\"' , ' ');
    
        stringstream ss (s);
        string key ;
        string val;
    
        ss>>title;
    
       while (ss)
        {
            ss>> key>>val;
            att.push_back(make_pair(key,val));
        } ;
        att.erase(att.end() -1 );
    }
    

    };

    int main(int argc, char const *argv[]) {

    int t , q;
    string line;
    getline(cin , line);
    istringstream input(line);
    input>>t>>q;
    vector<string> ta , qa;
    vector<Tag> tags;
    
    for (int i =0 ; i< t; i++) {
        string temp;
        getline(cin , temp);
        ta.push_back(temp);
    }
    
    for (int i =0 ; i< q; i++) {
        string temp;
        getline(cin , temp);
        qa.push_back(temp);
    }
    
    for (int i =0 ; i< ta.size(); i++) {
        Tag tag = Tag(ta[i]);
    
        tags.push_back(tag);
    }
    
    vector<string> currentParents;
        map<string , string> tagsQuires;
    
    for (int i =0 ; i< tags.size(); i++) {
        if (!tags[i].closeTag)
        {
        currentParents.push_back(tags[i].title);
        }
    
        for (int j = i+1 ; j < (tags.size() - (i + 1)); j++)
            {
                if (tags[j].title != tags[i].title )
                {
                    if (!tags[j].closeTag)
                    {
                     tags[i].children.push_back(tags[j]);
                    }
                }else{
    
                    j = tags.size();
                }   
            }
    
            if ( tags[i].closeTag )
                {
                    currentParents.erase(currentParents.end()-1);
                    // for (int k = 0; k < currentParents.size(); k++)
                    // {
                    //     cout<<currentParents[k]<<" ";
                    // }
                    // cout<<currentParents.size()<<" for "<<tags[i].title<<endl;
                    if (currentParents.size()> 0)
                    {
                        for (int l = 0; l < currentParents.size(); l++)
                        {
                            tagsQuires[tags[i].title] += (currentParents[l] +'.');
                        }
                        tagsQuires[tags[i].title] +=    tags[i].title;
                    }else{
                        tagsQuires[tags[i].title] =    currentParents[0];
                    }   
                }
    
         }
    
    
     for (auto i = tags.begin(); i != tags.end();
         ++i) {
        if ((*i).closeTag) {
            tags.erase(i);
            i--;
        }
    }
    
    for (int i =0 ; i< qa.size(); i++) {
    
        stringstream ss (qa[i]);
    
        string query;
        string qf = qa[i];
    
        string tag;
        string value;
        string result = "";
    
       while (getline(ss, query , '.')){
       };
        replace(query.begin() , query.end() , '~' , ' ');
        replace(qf.begin() , qf.end() , '~' , ' ');
    
        stringstream ssQuery (query);
        stringstream ssf(qf);
    
        ssQuery>>tag>>value;
        ssf>>qf;
    
        for (int j = 0; j < tags.size(); j++)
        {
            if (tags[j].title == tag)
            {
                vector< pair<string , string> > m = tags[j].att;
                for (int k = 0; k < m.size(); k++)
                {
                    if (m[k].first == value)
                    {
                        result = m[k].second;
                    }               
                } 
            }   
        }
    
        if (result != "" && tagsQuires[tag] == qf)
        {
          cout<<result<<endl;
        }else{
          cout<<"Not Found!"<<endl;
        }
    
    }
    return 0;
    

    }

    0|
    Permalink
  • danpetrukhin
    1 month ago+ 0 comments

    using a systematic approach...

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    static char c_bracket_open  = '<';
    static char c_bracket_close = '>';
    static char c_tag_close     = '/';
    static char c_equal         = '=';
    static char c_space         = ' ';
    static char c_quotas        = '"';
    static char c_tilda         = '~';
    static const char * s_tag_closing       = " >";
    static const char * s_query_delimiters  = ".~";
    static const char * s_erase_simbols     = " \t\n\r\f\v";
    static const char * s_error_message     = "Not Found!";
    
    namespace HRML
    {
    
    inline string & rtrim( string & s )
    {
        s.erase( s.find_last_not_of( s_erase_simbols ) + 1 );
        return s;
    }
    
    inline string & ltrim( string & s )
    {
        s.erase( 0, s.find_first_not_of( s_erase_simbols ));
        return s;
    }
    
    inline string & trim( string & s )
    {
        return ltrim( rtrim( s ));
    }
    
    class Object
    {
        public:
            bool operator==( const string & name ) const 
            { 
                return m_name == name; 
        }
                
            string m_name;
    };
    
    class Attribute : public Object
    {
        public:
            string m_value;
    };
    
    class Tag : public Object
    {
        public:
            vector< Attribute > m_vAttr;
            vector< Tag > m_vTag;
            
            Tag * parent = nullptr;
    };
    } // HRML
    
    using namespace HRML;
    
    
    int main() 
    {
        int tags = 0;
        cin >> tags;
    
        int queries = 0;
        cin >> queries;
        
        string str;
        std::getline( cin, str );
        
        vector< Tag > doc;
        Tag * current_tag = nullptr;
    
        for( int i = 0; i < tags; i++ )
        {
           std::getline( cin, str );
            
           if( str.empty() )
                continue;
    
            size_t n = 0;
    
            if( str[ n ] == c_bracket_open )
            {
                if( str[ n + 1 ] == c_tag_close )
                {
                    if( current_tag )
                        current_tag = current_tag->parent;
                    continue;
                }
                else
                {
                    Tag tag;
                    if( current_tag == nullptr )
                    {
                        doc.push_back( tag );
                        current_tag = & doc.back();                }
                    else
                    {
                        tag.parent = current_tag;
                        current_tag->m_vTag.push_back( tag );
                        current_tag = & current_tag->m_vTag.back();
                    }
    
                    n++;
                    auto pos = str.find_first_of( s_tag_closing, n );
                    current_tag->m_name = str.substr( n, pos - n );
    
                    if( str[ pos ] == c_bracket_close )
                    {
                        continue;
                    }
                    else
                    {
                        n = str.find_first_not_of( c_space, pos );
    
                        while( str[ n ] != c_bracket_close )
                        {
                            pos = str.find( c_equal, n );
                            
                            Attribute attr;
                            attr.m_name = str.substr( n, pos - n );
                            attr.m_name = trim( attr.m_name );
    
                            n = pos;
                            pos = str.find( c_quotas, n );
                            n = pos + 1;
                            pos = str.find( c_quotas, n );
    
                            attr.m_value = str.substr( n, pos -n );
                            n = pos + 1;
    
                            current_tag->m_vAttr.push_back( attr );
    
                            n = str.find_first_not_of( c_space, n );
                        }
    
                    }
                }
            }
        }
    
        for( int i = 0; i < queries; i++ )
        {
            std::getline( cin, str );
           
            size_t n = 0;
            current_tag = nullptr;
    
            auto pos = str.find_first_of( s_query_delimiters, n );
            
            while( pos != string::npos )
            {
                auto tag_name = str.substr( n, pos - n );
                n = pos + 1;
    
                auto & tags_vector = current_tag ? current_tag->m_vTag : doc;
    
                auto it = std::find( tags_vector.begin(),
                                     tags_vector.end(),
                                     tag_name );
                if( tags_vector.end() == it )
                {
                    pos = string::npos;
                    break;
                }
                else
                {
                    current_tag = & ( * it );
                }
    
                if( str[ pos ] == c_tilda )
                    break;
    
                pos = str.find_first_of( s_query_delimiters, n );
            }
    
            if( pos == string::npos )
            {
                cout << s_error_message << endl;
            }
            else
            {
                n = pos + 1;
    
                auto attr_name = str.substr( n, pos - n );
    
                auto it = std::find( current_tag->m_vAttr.begin(),
                                     current_tag->m_vAttr.end(),
                                     attr_name );
                if( current_tag->m_vAttr.end() != it )
                {
                    cout << ( * it ).m_value << endl;
                }
                else
                {
                    cout << s_error_message << endl;
                }
            }
        }
            
        return 0;
    }
    
    0|
    Permalink
  • devkantsah43
    1 month ago+ 1 comment

    how you all conqured that much level knowledge .What is your's source. I'm learning from 3 months and still i solved only 19 question

    0|
    Permalink
  • panbinwot
    1 month ago+ 0 comments

    Got it worked,

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <string>
    using namespace std;
    
    // struct Node{
    //     map<string,string> attr_map;
    //     map<string,Node> childMap;
    //     Node * parent = nullptr;
    //     string tag_name;
    // };
    
    string getHash(const vector<string> & vec){
        string hash  = "";
        for (auto c:vec){
            hash += c+".";
        }
        return hash;
    }
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        uint32_t N,Q;
        cin >> N >> Q;
        vector <string> stack; 
        bool listing_flg = true;
        
        map<string,map<string,string>> tagMap;
        
        for (int i=0;i<N;i++){
            
            string s;
            cin >> s;
            // string hash_ = getHash(stack);
            // cout << "|" << hash_ <<"\n";
            
            // Determine Starting Tag or Ending Tag
            if (s.find("/") != string::npos) {
                // Go to the next line if this is a end tag
                // string tag_name = s.substr(2, s.length()-3);
                
                // auto tail = stack.back();
                stack.pop_back();
                
            } else {
                // Starting Tag. Scan attributes
                map<string,string> attr_map;
                string hash_prefix = getHash(stack);
                if (s.find(">")==string::npos){
                    s = s.substr(s.find("<")+1);
                    tagMap[hash_prefix + s] = attr_map; 
                    stack.push_back(s);          
                } else {
                    // no attribute for this tag, skip following
                    auto start_pos = s.find("<"); 
                    auto end_pos = s.find(">"); 
                    tagMap[hash_prefix + s.substr(start_pos+1, end_pos-start_pos-1)] = attr_map;
                    stack.push_back(s.substr(start_pos+1, end_pos-start_pos-1)); 
                    continue;
                }
    
                string attr;
                cin >> attr;
                // ">" is end of the line
                bool val_flg = false;
                string attr_name;
                string attr_value;
                while (attr.find(">") == string::npos){
                    if (attr.compare("=")==0) {
                        attr.clear();
                        cin >> attr;
                        continue;
                    } else if (attr.find(">") != string::npos){
                        auto delimit_pos = attr.find("=");
                        attr_name = attr.substr(0, delimit_pos-1);
                        attr_value = attr.substr(delimit_pos+1, attr.length() - attr_name.length() - 2);
                        tagMap[hash_prefix+s][attr_name] = attr_value;
                        val_flg = false;
                    } else {
                        if (val_flg){
                            val_flg = false;                         
                            attr_value = attr.substr(1,attr.length()-2);
                            tagMap[hash_prefix+s][attr_name] = attr_value;
                        } else {
                            val_flg = true;
                            attr_name = attr;
                        }
                    }
                    attr.clear();
                    cin >> attr;    
                }
                
                // handle the last value
                attr_value = attr.substr(1,attr.length()-3);
                tagMap[hash_prefix+s][attr_name] = attr_value;
            }
        }
    
        //// Read query
        for (int i=0; i< Q; i++){
            string q;
            cin >> q;
            auto tilde_pos = q.find("~");
            auto hash_key = q.substr(0,tilde_pos);
            auto attr_name = q.substr(tilde_pos+1, q.length() - hash_key.length() - 1);
            
            // cout << hash_key << "|" << attr_name << '\n';
            if (tagMap.find(hash_key)==tagMap.end()){
                cout<< "Not Found!" << endl;
            } else {
                auto it = tagMap.find(hash_key);
                if (it->second.find(attr_name)==it->second.end()){
                    cout << "Not Found!" <<endl;
                } else {
                    auto it2 = it->second.find(attr_name);
                    cout << it2->second <<endl;
                    
                }
            }
        }
    
        return 0;
    }
    
    0|
    Permalink
  • tranlequocthong1
    1 month ago+ 0 comments

    The example was confusing me. I thought this just has single nested tag1 -> tag2 -> tag3 ... ->tagN, so I created a linked list and each node has a map contains attributes.

    0|
    Permalink
Load more conversations

Need Help?


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