【例题5-5 UVA 12096 】The SetStack Computer

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


用set来解决这个问题。
考虑如何表示
{ {{}} }这个集合
我们可以把{}这个集合和一个数字映射->1
然后把1加入到某个set里面去

{1}
则这就对应了->{ {} }
然后把{1}也用一个int对应->2
然后把2加入一个集合
{2}
则这个就对应了->{ {{}} }
这样,每个集合都能用set来表示了.
则用map < set ,int>来搞映射。
然后正常地用set求交集、并集就好了。
->交集并集set都有现成的函数的。
栈里面可以只存这个set的映射就行。

【错的次数】


在这里输入错的次数

【反思】


这种一层套一层的方法好巧妙。。

【代码】

#include <bits/stdc++.h>
using namespace std;

typedef set<int> Set;

map <Set, int> mymap;
stack <int> sta;
vector <Set> what;

int ID(Set x)
{
    if (mymap.find(x) != mymap.end()) return mymap[x];
    what.push_back(x);
    mymap[x] = (int)what.size() - 1;
    return mymap[x];
}

int main()
{
    //freopen("F:\\rush.txt", "r", stdin);
    ios::sync_with_stdio(0), cin.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        what.clear();
        while (!sta.empty()) sta.pop();
        mymap.clear();
        int n;
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            string s;
            cin >> s;
            if (s[0] == 'P')
                sta.push(ID(Set()));
            else
                if (s[0] == 'D')
                    sta.push(sta.top());
                else
                {
                    Set x = what[sta.top()]; sta.pop();
                    Set y = what[sta.top()]; sta.pop();
                    Set temp;
                    if (s[0] == 'U')
                        set_union(x.begin(), x.end(), y.begin(), y.end(), inserter(temp, temp.begin()));
                    if (s[0] == 'I')
                        set_intersection(x.begin(), x.end(), y.begin(), y.end(), inserter(temp, temp.begin()));
                    if (s[0] == 'A')
                    {
                        temp = y;
                        temp.insert(ID(x));
                    }
                    sta.push(ID(temp));
                }
            cout << (int)what[sta.top()].size() << endl;
        }
        cout << "***" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值