Attribute Parser

  • + 0 comments

    here is my solution for this problem:

    typedef struct  
    {
        string tag;
        string value;
    } TagAttr;
    
    int main() 
    {
        int N, Q;
        
        cin >> N >> Q;
        cin.ignore();
        
        string tag, line;
        vector<TagAttr> tagsAttr;
        int currentPos;
    
        for(int n = 0; n < N; n++)
        {
            currentPos = 1;
            
            getline(cin, line);
            if(line[currentPos] == '/')
            {
                if(tag.rfind('.') != string::npos)
                {
                    tag = tag.substr(0, tag.rfind('.'));
                }
                else
                {
                    tag.clear();
                }                
            }
            else
            {
                TagAttr element;
                int i = 0;
                
                while(line[currentPos+i] != ' ' && line[currentPos+i] != '>')
                    i++;
                
                if(tag.empty())
                    tag = line.substr(currentPos, i);
                else
                    tag = tag + '.' + line.substr(currentPos, i);
                    
                currentPos += i;
                
                while (line[currentPos] != '>') 
                {
                    currentPos++;
                    i=0;
                    
                    while(line[currentPos+i] != ' ')
                        i++;
                        
                    element.tag = tag + "~" + line.substr(currentPos, i);
                    
                    currentPos += (i+3);
                    i=0;
                    
                    while(line[currentPos+i] != ' ' && line[currentPos+i] != '>')
                        i++;
                    
                    element.value = line.substr(currentPos+1, i-2);
                    
                    currentPos += i;
                                        
                    i=0;
                    tagsAttr.push_back(element);
                }
            }
        } 
        
        string query;
        while (Q--) 
        {
            getline(cin, query);
            
            int i = 0;
            for(auto &elm : tagsAttr)
            {
                i++;
                if(query.compare(elm.tag) == 0)
                {
                    cout << elm.value << endl;
                    break;
                }
                if(i == tagsAttr.size())
                    cout << "Not Found!" << endl;
            }
        }
        return 0;
    }