1

试图解决一个中等难度的问题。这是新手代码。我基本上尝试为一种虚构的标记语言编写一个属性解析器,您应该在其中通过查询来检索 a 标签的属性值。

我尝试实现一个嵌套哈希表(unordered_map)来将标签的名称映射到它的属性键值对。(tagName -> (attrName, attrValue))

https://www.hackerrank.com/challenges/attribute-parser/problem?isFullScreen=true 链接到问题^

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

#include <string>
#include <unordered_map>
#include <array>
#include <sstream>
using namespace std;

string reverse(string str, int begin, int end) {
    str = str.substr(begin, end - begin);

    string res;
    for (int i = str.length() - 1; i >= 0; --i) {
        res += str[i];
    }
    return res;
}

int main() {
    // HASH TABLE
    unordered_map<string, unordered_map<string, string>> umap;
    unordered_map<string, string> imap;

    // N, Q
    int n, q;
    cin >> n >> q;

    // parse and save: tag name, attr name, attr value
    for (int i = 0; i < n/2; ++i) {
        // parse input string
        string input;
        getline(cin, input);
        stringstream ssin(input);
        array<string, 4> vals;
        // <tagName ; attrName ; = ; attrValue
        // parse 4 clauses 
        int j = 0;
        while (j < vals.size()) {
            ssin >> vals[j];
            ++j;
        }
        // preprocess input clauses: (1)tagName ; (2)attrName ; (3)attrValue
        string tagName = vals[0].substr(1);
        string attrName = vals[1];
        string temp_attrValue = vals[3].substr(1);
        temp_attrValue = reverse(temp_attrValue, 1, temp_attrValue.length());
        string attrValue = reverse(temp_attrValue, 2, temp_attrValue.length());

        // add preprocessed clauses to hash-table
        imap.insert(pair<string, string>(attrName, attrValue));
        umap.insert(pair<string, unordered_map<string, string>>(tagName, imap));
    }
    // loop through rest of source code
    for (int i = 0; i < n/2; ++i) {
        string input;
        getline(cin, input);
    }

    // queries
    for (int i = 0; i < q; ++i) {
        //preprocess clauses: (1)tagName ; (2)attrName
        string query;
        getline(cin, query);
        stringstream ss(query);
        string segment;
        vector<string> segList;
        while (getline(ss, segment, '~')) {
            segList.push_back(segment);
        }

        // condition if tagName is a nested subtag
        short res = 0;
        for (int i = 0; i < segList[0].length(); ++i) {
            if (segList[0][i] == '.') {
                ++res;
            }
        }
        // QUERIED VALUES
        string queryTagName = segList[0];
        string queryAttrName = segList[1];

        // if there exists '.', parse tagName from ID
        if (res > 0) {
            string ID = segList[0];
            stringstream ss(ID);
            string seg;
            vector<string> segs;
            while (getline(ss, seg, '.')) {
                segs.push_back(seg);
            }
            queryTagName = segs[segs.size() - 1];
        }

        // OUTPUT ==============
        unordered_map<string, unordered_map<string, string>>::iterator u_itr = umap.find(queryTagName);
        if (umap.find(queryTagName) == umap.end()) {
            cout << "Not Found!" << endl;
        } else {
            if (imap.find(queryAttrName) == imap.end()) {
                cout << "Not Found!" << endl;
            } else {
                cout << u_itr->second.find(queryAttrName)->second << endl;
            }
        }
    }

    return 0;
}

我不断收到“超出范围”错误。有人知道这里出了什么问题吗?

4

1 回答 1

0

string tagName = vals[0].substr(1);由于您vals是空的,因此您会遇到错误。要验证此替换string tagName = vals[0].substr(1);string tagName = vals[0].substr(0);您将不会收到相同的错误。

于 2020-01-05T10:09:37.090 回答