Uva 12096 The SetStack Computer

题意:模拟栈的情况,但是这个栈处理的元素是集合。 紫书的思路
将所有集合都编号,用map来进行编号。

set 中的set_union() 取a,b并集到c中去
set_intersection() 同时包含a,b中的元素,将其复制到c中去

#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
typedef set<int> Set;
map<Set,int> IDcache;
vector<Set> Setcache;

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

int n,t;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        char op[15];
        //IDcache.clear();
        //Setcache.clear();
        stack<int> S;
        while(n--)
        {
            scanf("%s",op);
            if(op[0] == 'P')
                S.push(Id(Set()));
            else if(op[0] == 'D')
                S.push(S.top());
            else
            {
                Set s1 = Setcache[S.top()]; S.pop();
                Set s2 = Setcache[S.top()]; S.pop();
                Set x;
                if(op[0] == 'U')    set_union(ALL(s1),ALL(s2),INS(x));
                if(op[0] == 'I')    set_intersection(ALL(s1),ALL(s2),INS(x));
                if(op[0] == 'A')
                {
                    x = s2; x.insert(Id(s1));
                }
                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、付费专栏及课程。

余额充值