The SetStack Computer UVA - 12096

29 篇文章 0 订阅

UVA - 12096

由于这个题目集合不是一般的集合,是集合的集合,因此给每个不同的集合分配一个唯一确定的ID,则将集合的集合转化为ID的集合。//思路来源于刘汝佳《算法竞赛入门经典》P116

#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;

int n,T;
typedef set<int> Set;
map<Set,int>Set_ID; //集合 -> ID
vector<Set> ID_Set; //ID -> 集合

int count_ID(Set x){ //根据集合x查找其ID。如没有则分配新ID
    if(Set_ID.count(x)) return Set_ID[x];
    ID_Set.push_back(x);//添加新的集合
    return Set_ID[x] = ID_Set.size()-1;
}
int main(){
    cin>>T;
    while(T--){
        cin>>n;
        string s;
        stack<int> ans;
        while(n--){
            cin>>s;
            if(s[0]=='P') ans.push(count_ID(Set())); //Set()<->set<int>() ->空集
            else if(s[0]=='D'){
                int x = ans.top();
                ans.push(x);
            }else{
                Set x,x1,x2;
                x1 = ID_Set[ans.top()]; ans.pop();
                x2 = ID_Set[ans.top()]; ans.pop();
                if(s[0]=='U')set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x,x.begin()));
                if(s[0]=='I') set_intersection(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x,x.begin()));
                if(s[0]=='A'){
                    x = x2;
                    x.insert(count_ID(x1));
                }
                ans.push(count_ID(x));
            }
            cout<<ID_Set[ans.top()].size()<<endl;
        }
        cout<<"***"<<endl;
        Set_ID.clear();
        ID_Set.clear();
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值