12096 - The SetStack Computer

这道题主要考的STL一些用法吧。。。。做不出来就是对STL的一些函数不认识

主要学习了

set_union(c1.begin(),c1.end(),c2.begin(),c2.end(),inserter(c,c.begin());

set_intersection(c1.begin(),c1.end(),c2.begin(),c2.end(),inserter(c,c.begin());

这个2个STL函数用来取 并 交 集合

#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<list>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<iostream>
using namespace std;
typedef set<int> Set;
#define INS(x) inserter(x,x.begin())
#define ALL(x) x.begin(),x.end()
map<Set,int>ID_array;
vector<Set>Set_array;
int Find_ID(Set x){
    if(ID_array.find(x) != ID_array.end())
       return ID_array[x];
    else {
        Set_array.push_back(x);
        return ID_array[x] = Set_array.size() - 1;
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        stack<int>st;
        int n;
        scanf("%d",&n);
        for(int i = 0 ; i < n ; i ++){
            string s;
            cin >> s;
            if(s == "PUSH")     st.push(Find_ID(set<int>()));
            else if(s == "DUP") st.push(st.top());
            else {
                Set x1 = Set_array[st.top()]; st.pop();
                Set x2 = Set_array[st.top()]; st.pop();
                Set x;
                if(s == "UNION")     set_union(ALL(x1),ALL(x2),INS(x));
           else if(s == "INTERSECT") set_intersection(ALL(x1),ALL(x2),INS(x));
           else if(s == "ADD"){
               x = x2;
               x.insert(Find_ID(x1));
           }
           st.push(Find_ID(x));
        }
            printf("%d\n",Set_array[st.top()].size());
        }
        printf("***\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值