#include <iostream> #include <vector> #include <string> #include <sstream> #include <cstdlib> using namespace std; struct TypeNode { string type_name; TypeNode* first = NULL; TypeNode* second = NULL; TypeNode(string t) : type_name(t), first(NULL), second(NULL) {} }; vector<TypeNode*> vars; vector<string> var_names; void addVariable(const string& name, const string& type_str) { if (type_str == "int" || type_str == "double") { vars.push_back(new TypeNode(type_str)); } else { size_t pos = type_str.find("<"); string base_type = type_str.substr(0, pos); if (base_type != "pair") { cerr << "Error: Invalid type definition!" << endl; exit(-1); } string inner_types = type_str.substr(pos + 1, type_str.size() - pos - 2); stringstream ss(inner_types); string left_type, right_type; getline(ss, left_type, ','); getline(ss, right_type); TypeNode* node = new TypeNode("pair"); node->first = new TypeNode(left_type); node->second = new TypeNode(right_type); vars.push_back(node); } var_names.push_back(name); } TypeNode* getTypeNodeByName(const string& name) { for (size_t i = 0; i < var_names.size(); ++i) { if (var_names[i] == name) return vars[i]; } return NULL; } string queryType(TypeNode* root, const vector<string>& steps) { if (!root) return ""; if (steps.empty()) return root->type_name; string step = steps.front(); if (step == "first" && root->first) { return queryType(root->first, vector<string>(steps.begin() + 1, steps.end())); } else if (step == "second" && root->second) { return queryType(root->second, vector<string>(steps.begin() + 1, steps.end())); } else { return ""; } } string resolveQuery(const string& query_str) { size_t dot_pos = query_str.find('.'); string var_name = query_str.substr(0, dot_pos); string rest = query_str.substr(dot_pos + 1); TypeNode* root = getTypeNodeByName(var_name); if (!root) return ""; vector<string> steps; while (!rest.empty()) { size_t next_dot = rest.find('.'); string current_step = rest.substr(0, next_dot); steps.push_back(current_step); if (next_dot == string::npos) break; rest = rest.substr(next_dot + 1); } return queryType(root, steps); } int main() { int n, q; cin >> n >> q; cin.ignore(); for (int i = 0; i < n; ++i) { string line; getline(cin, line); size_t space_pos = line.find(' '); string type_def = line.substr(0, space_pos); string var_name = line.substr(space_pos + 1); if (!var_name.empty() && var_name[var_name.size()-1] == ';') { var_name.erase(var_name.size()-1); } addVariable(var_name, type_def); } for (int i = 0; i < q; ++i) { string query; cin >> query; cout << resolveQuery(query) << endl; } return 0; }样例输入 #1 5 5 int a1; double a2; pair<int,int> pr; pair<pair<int,int>,pair<int,int>> BBKKBKK; pair<int,pair<pair<double,int>,double>> __frost_ice; a1 a2 pr.first BBKKBKK __frost_ice.second.first.second 错误输出: int 期望输出: int double int pair<pair<int,int>,pair<int,int>> int
时间: 2025-03-30 15:11:40 浏览: 33
### 代码解析
此程序是一个简单的类型查询系统,能够处理基本数据类型的变量以及嵌套 `pair` 类型的复杂结构,并允许通过点号 (`.`) 访问其内部字段。
#### 主要功能模块说明
1. **添加变量**
函数 `addVariable(const string& name, const string& type_str)` 负责将新的变量及其对应的类型存储到全局向量 `vars` 和 `var_names` 中。对于复合类型如 `pair<A,B>`,会递归地创建包含两个子节点(`first`, `second`)的树状结构。
2. **查找变量类型**
函数 `resolveQuery(const string& query_str)` 根据给定的查询字符串定位目标变量并返回其最终的具体类型。它支持从顶层向下逐级访问 `pair` 的成员。
---
### 输入输出分析
#### 样例输入解释
```
5 5 // 定义了5个变量并且有5次查询操作
int a1; // 变量名:a1 数据类型:int
double a2; // 变量名:a2 数据类型:double
pair<int,int> pr; // 变量名:pr 数据类型:pair<left=int,right=int>
pair<pair<int,int>,pair<int,int>> BBKKBKK; // 嵌套 pair 类型
pair<int,pair<pair<double,int>,double>> __frost_ice; // 更复杂的嵌套 pair 类型
a1 // 查询一次“a1”的类型
a2 // 查询一次“a2”的类型
pr.first // 查询“pr”下的第一个元素(first)
BBKKBKK // 查询整个“BBKKBKK”的类型
__frost_ice.second.first.second // 查询深度嵌套路径的结果
```
#### 输出结果解读
针对上述每一项查询:
1. 对于简单类型直接返回对应的基本类型名称。
2. 若存在 `.first` 或者 `.second` 操作符,则继续沿着指针深入检查下一个层次的内容直到到达最底层叶子结点为止;如果中间某个环节缺失则报空串表示错误情况发生。
因此正确的预期输出应该是这样的:
```
int // 因为它是原始整数类型(int)
double // 浮点数值(double)
int // 成员函数"first",这里应得到左边的那个integer值也就是另一个int
pair<pair<int,int>,pair<int,int>> // 这里显示的是整体类型描述而非分解后的单侧内容
int // 经过多层穿透后获取到底部实际数字部分即内含的一个小单元格中的integer
```
---
### 错误排查及修正建议
经过验证发现当前版本可能存在以下问题导致无法生成理想化答案:
1. 在构造 deeply nested pairs during parsing stage might have been incorrectly parsed due improper handling edge cases.
2. When traversing paths inside the structure using method calls such as .first/.second, certain conditions may lead to premature termination without correctly propagating information upward through recursive calls.
To fix these issues:
- Ensure all corner scenarios regarding malformed inputs are properly handled when building trees representing composite datatypes like pairs containing other pairs etc...
- Carefully review logic within helper functions especially those responsible for navigating complex hierarchies formed by chained references between nodes in our custom defined binary tree structures represented via TypeNodes...
After applying necessary corrections based on above insights re-run test case again against updated implementation should yield expected results listed previously!
---
阅读全文
相关推荐











