The SetStack Computer (map+set+vector)

本题最有价值的部分是这个ID函数

map<Set, int>IDcache;
vector<Set>Setache;
int ID(Set s)
{
    if (IDcache.count(s))return IDcache[s];
    Setache.push_back(s);
    return IDcache[s] = Setache.size() - 1;
}

通过这个函数可以讲任意变量映射为一个int值,并通过这个int值可以在vector中查找到映射值所对的变量

ac代码

#include<iostream>
#include<cstring>
#include<queue>
#include<set>
#include<algorithm>
#include<sstream>
#include<map>
#include<stack>
#include<vector>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
const int  N = 1e5 + 10;
using namespace std;
#define FAST ios::sync_with_stdio(false)
#define int long long 
#define endl '\n'
typedef set<int>Set;
map<Set, int>IDcache;
vector<Set>Setache;
stack<int>s;
int ID(Set s)
{
    if (IDcache.count(s))return IDcache[s];
    Setache.push_back(s);
    return IDcache[s] = Setache.size() - 1;
}
signed main()
{
    FAST;
    int _;
    cin >> _;
    while (_--)
    {
        Setache.clear();
        IDcache.clear();
        while (!s.empty())s.pop();
        int n;
        cin >> n;
        while (n--)
        {
            string op;
            cin >> op;
            if (op[0] == 'P')s.push(ID(Set()));
            else if (op[0] == 'D')s.push(s.top());
            else {
                Set x1 = Setache[s.top()]; s.pop();
                Set x2 = Setache[s.top()]; s.pop();
                Set x;
                if (op[0] == 'U')set_union(ALL(x1), ALL(x2), INS(x));
                if (op[0] == 'I')set_intersection(ALL(x1), ALL(x2), INS(x));
                if (op[0] == 'A') { x = x2; x.insert(ID(x1)); }
                s.push(ID(x));
            }
            cout << Setache[s.top()].size() << endl;
        }
        cout << "***" << endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值