需要帮助调试"attribute parser"!C++ 来自黑客排名的问题
Need help debugging "attribute parser"! C++ Question from HackerRank
试图解决一个中等难度的问题。这是新手代码。 我基本上尝试为一种虚构的标记语言编写一个属性解析器,你应该通过查询来检索标签的属性值。
我尝试实现一个嵌套哈希表 (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);
,您不会收到相同的错误。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 需要帮助调试"attribute parser"!C++ 来自黑客排名的问题
- 这是一个二叉搜索树吗?黑客排名问题
- 在黑客地球上没有获得以下C ++代码问题的输出
- 在C++中解决最小掉期黑客排名问题
- 优化:黑客地球邮递员软件工程师实习生问题
- 得到~对标准输出没有回应~在黑客排名问题上
- 为什么在黑客兰克的许多问题中都会完成拆分字符串
- 黑客排名最小最大值问题,最大总和给出错误的负值
- 如何修复问题中的"teminated due to timeout"错误 数组操作 |黑客兰克?
- 围绕黑客排名的问题 30 天代码,第 6 天 [C++]
- 黑客世界,霍格沃茨的女巫们.我的方法有什么问题?