Attribute Parser

  • + 0 comments

    int main() { int N, Q; cin >> N >> Q;

    cin.ignore();   
    vector<string> input; 
    vector<string> query; 
    unordered_map<string, string> solution; 
    
    for (int i = 0; i < N; i++) {
        string str;
        getline(cin, str); 
        input.push_back(str); 
    }
    
    for (int i = 0; i < Q; i++) {
        string str; 
        getline(cin, str); 
        query.push_back(str); 
    }
    
    string cur = ""; 
    for (int i = 0; i < N; i++) {
        vector<string> tokens; 
        string line = input[i]; 
        char *cstr = new char[line.length() + 1];
        strcpy(cstr, line.c_str());
        char *token = strtok(cstr, "<=\"> "); 
        while (token != nullptr) {
            tokens.push_back(string(token));
            token = strtok(nullptr, "<=\"> ");
        }
        delete [] cstr; 
        if (tokens[0][0] != '/') {
            cur += tokens[0] + "."; 
        }
        else {
            int size = tokens[0].size(); 
            while (size--) {
                cur.pop_back(); 
            }
        }
    
        string prev = cur; 
        prev[prev.size() - 1] = '~'; 
        for (int j = 2; j < tokens.size(); j += 2) {
            solution[prev + tokens[j - 1]] = tokens[j];  
        }
    }
    
    for (int i = 0; i < Q; i++) {
        if (solution.find(query[i]) == solution.end()) {
            cout << "Not Found!\n"; 
        } 
        else {
            cout << solution[query[i]] << "\n"; 
        }
    }
    
    return 0; 
    

    }