UVA12096 - The SetStack Computer

stack的用法

#include <iostream>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int MAXN = 2010;
const int N = 20;

int cnt;
stack<set<int> > stk;
map<set<int>, int> mp;
set<int> s1,s2;

void pop(){
    s1 = stk.top(); stk.pop();
    s2 = stk.top(); stk.pop();
}

void Push(){
    set<int> s;
    stk.push(s);
    cout<<"0"<<endl;
}

void Dup(){
    set<int> s;
    s = stk.top(); stk.push(s);
    cout<<(int)s.size()<<endl;
}

void Union(){
    pop();
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) s2.insert(*it);
    stk.push(s2);
    cout<<(int)s2.size()<<endl;
}

void Intersect(){
    pop();
    set<int> s3;
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
        if (s2.find(*it) != s2.end()) s3.insert(*it);
    stk.push(s3);
    cout<<(int)s3.size()<<endl;
}

void Add(){
    pop();
    if (s1.empty()) s2.insert(0);
    else {
        if (!mp[s1]) mp[s1] = cnt++;
        s2.insert(mp[s1]);
    }
    stk.push(s2);
    cout<<s2.size()<<endl;
}

int main(){
    int t,n;
    char str[N];
    cin>>t;
    while (t--){
        cin>>n;
        while (!stk.empty())
            stk.pop();
        cnt = MAXN;
        mp.clear();
        while (n--){
            cin>>str;
            if (str[0] == 'P')
                Push();
            else if (str[0] == 'D')
                Dup();
            else if (str[0] == 'U')
                Union();
            else if (str[0] == 'I')
                Intersect();
            else Add();
        }
        cout<<"***"<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值