UVA12096 the SetStack Computer(STL中,map,set,vector,stack联合应用)

题目连接:

传送门

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<stack>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
using namespace std;
//把set定义成类型
typedef set<int> Set;
//map的作用是,不同的集合,都映射一个值
map<Set,int> mmp;
//vector是对于给出的map的值,能在这里找到对应的set
vector<Set> vec;
//stack 模拟题目中的栈,而里面存的是int型,它的下标可根据vector里面的set,可以找到对应其的集合,map在其中起连接作用
stack<int>sta;
// isset函数的作用是判断集合x是否在map中,如果在,就返回他多对应的值,如果不在,加入vector中,并在map中建立相应的映射关系
int isset(Set x)
{
    if(mmp.count(x)) return mmp[x];
    vec.push_back(x);
    return mmp[x]=vec.size()-1;
}
int  main()
{
    int T;
    cin>>T;
    while(T--){
    //初始化
        mmp.clear();
        while(!sta.empty())sta.pop();
        vec.clear();
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            string str;
            cin>>str;
            if(str=="PUSH")sta.push(isset(Set()));
            else if(str=="DUP") sta.push(sta.top());
            else
            {
                Set x1=vec[sta.top()];sta.pop();
                Set x2=vec[sta.top()];sta.pop();
                Set x;
                if(str=="UNION") set_union(ALL(x1),ALL(x2),INS(x));
                if(str=="INTERSECT") set_intersection(ALL(x1), ALL(x2), INS(x));
                if(str=="ADD") {
                    x=x2;x.insert(isset(x1));
                }
                sta.push(isset(x));
            }
            cout<<vec[sta.top()].size()<<endl;
        }
            cout<<"***"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值