欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > 牛客std:pair,指针+递归+整体法的使用

牛客std:pair,指针+递归+整体法的使用

2024/10/24 1:49:57 来源:https://blog.csdn.net/Colinnian/article/details/141859416  浏览:    关键词:牛客std:pair,指针+递归+整体法的使用

登录—专业IT笔试面试备考平台_牛客网 

#include <bits/stdc++.h>
using ll = long long;constexpr int PAIR = 0;
constexpr int INT = 1;
constexpr int DOUBLE = 2;
struct Type {int base;Type *first;Type *second;
};Type *readType(const std::string &s, int &i) {Type *t = new Type;if (s[i] == 'i') {i += 3;t->base = INT;} else if (s[i] == 'd') {i += 6;t->base = DOUBLE;} else {i += 5;t->first = readType(s, i);i++;t->second = readType(s, i);i++;}return t;
}void printType(Type* t) {if (t->base == INT) {std::cout << "int";}else if (t->base == DOUBLE) {std::cout << "double";}else {std::cout << "pair<";printType(t->first);std::cout << ",";printType(t->second);std::cout << ">";}
}int main() {std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);int n, q;std::cin >> n >> q;std::vector<Type *> type(n);std::vector<std::string> name(n);std::map<std::string, int> id;for (int i = 0; i < n; i++) {std::string t;std::cin >> t >> name[i];name[i].pop_back();int cur = 0;type[i] = readType(t, cur);id[name[i]] = i;}while (q--) {std::string s;std::cin >> s;s += ".";std::vector<std::string> a;for (int i = 0; i < s.size(); i++) {int j = s.find('.', i);a.push_back(s.substr(i, j - i));i = j;}Type* t = type[id[a[0]]];for (int i = 1; i < a.size(); i++) {if (a[i] == "first") {t = t->first;}else {t = t->second;}}printType(t);std::cout << "\n";}return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com