UVA - 12096 The SetStack Computer STL的大集合

题目大意:给出几种操作
PUSH:在栈顶压入一个空集
DUP:把栈顶的集合复制一下在放入栈顶
ADD:取出栈顶两个集合相加,再把结果放入栈顶
UNION:取出栈顶两个集合,取这两个集合的并集并放入栈顶
INTERSET:取出栈顶两个集合,取这两个集合的交集并放入栈顶

解题思路:用STL

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef set<int> Set;
map<Set, int> IDcache; vector<Set> Setcache;

int ID(Set x) {
    if(IDcache.count(x))
        return IDcache[x];
    Setcache.push_back(x);
    return IDcache[x] = Setcache.size() - 1;
}

int main() {
    int test, n;
    char str[20];
    scanf("%d", &test);
    while(test--) {
        stack<int> s;
        int n;
        scanf("%d", &n);
        while(n--) {
            scanf("%s", str);
            if(strcmp(str,"PUSH") == 0) {
                s.push(ID(Set()));  
            }
            else if(strcmp(str,"DUP") == 0) 
                s.push(s.top());
            else {
                Set x1 = Setcache[s.top()];
                s.pop();
                Set x2 = Setcache[s.top()];
                Set x;
                s.pop();
                if(strcmp(str,"UNION") == 0) 
                    set_union(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
                if(strcmp(str,"INTERSECT") == 0)
                    set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
                if(strcmp(str,"ADD") == 0) {
                    x = x2;
                    x.insert(ID(x1));   
                }
                s.push(ID(x));
            }
            printf("%d\n",Setcache[s.top()].size());
        }
        printf("***\n");
    }
    return 0;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值