牛客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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值