紫书章五例题五 集合栈计算机 UVA 12096(stack)

这个题的特点进行集合运算,stack中放的是集合。
然后我们用vector将集合存起来,将对应的下标来代表集合。用map来实现对应关系。其中使用set来存一个集合,因为集合中不能有重复的元素。
stack a
a.top();
a.push();
a.pop();
a.empty();

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <stack>
#include <map>
#include <vector>
using namespace std;
//typedef set<int> Set;
char s[30];
set<int> t;
map<set<int>,int> M;
stack<int> sta;
vector< set<int> > v;
int num(set<int> k)
{
    if(M.count(k)) return M[k];
    v.push_back(k);
    M[k]=v.size()-1;
    return M[k];
}
int main()
{
  //  freopen("E:\\input.txt","r",stdin);
    int n;
    scanf("%d",&n);
    while(n--){
        v.clear();
        while(!sta.empty()) sta.pop();
        M.clear();
        int m;
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%s",s);
            if(s[0]=='P')
                sta.push(num(t));
            else if(s[0]=='D')
                sta.push(sta.top());
            else
            {
                int a=sta.top();sta.pop();
                int b=sta.top();sta.pop();
                set<int> t1=v[a],t2=v[b],t3;
                if(s[0]=='A'){
                    t3=t2;
                    t3.insert(num(t1));
                    sta.push(num(t3));
                }
                else if(s[0]=='U'){
                    set<int> ::iterator it;
                    t3=t2;
                    for(it=t1.begin();it!=t1.end();it++)
                        t3.insert(*it);
                    sta.push(num(t3));
                }
                else if(s[0]=='I'){
                    set<int> ::iterator it;
                    for(it=t1.begin();it!=t1.end();it++){
                        if(t2.count(*it)) t3.insert(*it);
                    }
                    sta.push(num(t3));
                }
            }
            int g=sta.top();
            cout<<v[g].size()<<endl;
        }
        cout<<"***"<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值