算法竞赛入门经典第五章例题5-5 The SetStack Computer UVA - 12096

这篇博客详细解析了算法竞赛中UVA 12096题目——The SetStack Computer。作者通过实例探讨如何对每个集合进行编号,并利用这些编号构建问题解决方案,采用类表示法来描述集合操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://vjudge.net/problem/UVA-12096
对每一个集合进行编号,那么任一个集合就是编号的集合,既可用set<int>类表示

#include<iostream>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<string>
using namespace std;
#pragma warning(disable:4996)
typedef set<int> Set;
map<Set, int> IDcache;
vector<Set> Setcache;
int ID(Set x) {
    if (IDcache.count(x)) return IDcache[x];
    Setcache.push_back(x);
    return IDcache[x] = Setcache.size() - 1;
}
stack<int> s;
int T,N;
string str;
int main(){
#ifdef _DEBUG
    freopen("in", "rb", stdin);
    //freopen("out", "wb", stdout);
#endif // _DEBUG
    cin >> T;
    while (T--) {
        cin >> N;
        while (N--) {
            cin >> str;
            if(str[0]=='P') s.push(ID(Set()));
            else if(str[0]=='D') s.push(s.top());
            else{
                Set x1 = Setcache[s.top()]; s.pop();
                Set x2 = Setcache[s.top()]; s.pop();
                if (str[0] == 'U')  x1.insert(x2.begin(), x2.end()), s.push(ID(x1));
                else if (str[0] == 'I') {
                    auto it = x1.begin();
                    while (it != x1.end()) {
                        if (!x2.count(*it)) it = x1.erase(it);
                        else ++it;
                    }
                    s.push(ID(x1));
                }
                else x2.insert(ID(x1)),s.push(ID(x2));
            }
            cout << Setcache[s.top()].size() << endl;
        }
        cout << "***" << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值