需要帮助调试"attribute parser"!C++ 来自黑客排名的问题

Need help debugging "attribute parser"! C++ Question from HackerRank

本文关键字:黑客 问题 帮助 调试 attribute parser C++      更新时间:2023-10-16

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

我尝试实现一个嵌套哈希表 (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;
}

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

您在string tagName = vals[0].substr(1);时遇到错误,因为您的vals为空。 要验证这一点,请将string tagName = vals[0].substr(1);替换为string tagName = vals[0].substr(0);,您不会收到相同的错误。