Attribute Parser

  • + 0 comments
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #include <sstream>
    #include <map>
    #include <queue>
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        priority_queue<string> keys;
        map<string, map<string, string>> data;    
        int dataCount;
        int queryCount;
        
        // read the number of data and query
        string line;
        getline(cin, line);
        istringstream iss(line);
        iss >> dataCount >> queryCount;
        
        for(int i = 0; i < dataCount; i++)
        {
            getline(cin, line);
            line.pop_back();
            istringstream iss(line);
            string token;
            
            string key = "";
            string value = "";
            string operation = "";
            
            iss >> token;
            if(token[1] == '/')
            {
                keys.pop();
                continue;
            }
            
            if(token[0] == '<')
            {
                if(keys.empty())
                {
                    keys.push(token.substr(1));
                }
                else
                {
                    keys.push(keys.top() + '.' + token.substr(1));
                }
            }
            while(iss >> token)
            {
                if(key == "")
                {
                    key = token;
                    continue;
                }
                
                if(operation == "")
                {
                    operation = token;
                    continue;
                }
                
                if(value == "")
                {
                    value = token;
                    data[keys.top()][key] = value.substr(1, value.length()-2); 
                    key = "";
                    operation = "";
                    value = "";
                    continue;
                }
                
            }
        }
        for(int i = 0; i < queryCount; i++)
        {
            getline(cin, line);
            size_t delimiterPos = line.find('~');
            
            if(data[line.substr(0, delimiterPos)][line.substr(delimiterPos + 1)].empty())
            {
                cout << "Not Found!" << endl;
            }
            else
            {
                cout << data[line.substr(0, delimiterPos)][line.substr(delimiterPos + 1)] << endl;
            }
            
        }
        return 0;
    }