算典05_例题_05_UVA-12096

The SetStack Computer

题意

有一个专门为了集合运算而设计的“集合栈”计算机。该机器有一个初始为空的栈,并且支持以下操作:

PUSH:空集“{}”入栈
DUP:把当前栈顶元素复制一份后再入栈
UNION:出栈两个集合,然后把两者的并集入栈
INTERSECT:出栈两个集合,然后把二者的交集入栈
ADD:出栈两个集合,然后把先出栈的集合加入到后出栈的集合中,把结果入栈

每次操作后,输出栈顶集合的大小(即元素个数)。
例如栈顶元素是A={ {}, {{}} }, 下一个元素是B={ {}, {{{}}} },则:
UNION操作将得到{ {}, {{}}, {{{}}} },输出3.
INTERSECT操作将得到{ {} },输出1
ADD操作将得到{ {}, {{{}}}, { {}, {{}} } },输出3.

题解

这道题对于理解map和set甚是有用
说白了,这道题的核心是如何表示“集合的集合”
如果我们直接用set < set<> > 会很麻烦,这里不妨只保存集合的下标,即用一个set < int > 来表示某个集合,它里面的元素为其他集合的下标,那么它就是一个集合的集合了
但是还有一点很重要,如果两个集合中的元素是一样的,那么我们认为它们是同一个集合,即它们有同一个下标,这样才能保证集合交和并的正确性
所以我们需要一个映射map < Set, int > m 来表示一个集合到下标的映射,这样只要判断m.count(某个Set)即可知道这个集合是否出现过了,只有当它从未出现过的时候才给它新的编号
这是我自己写的代码,也AC了,但是不如书上用的巧妙

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <map>

using namespace std;
const int maxn = 2e6 + 5;
const int INF = (1<<31)-1;
#define met(a, b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
typedef long long LL;
typedef set<int> Set;

int t, n;
char op[10];
int stake[maxn], top, sid;
Set s[maxn];
map<Set, int> m;

void solve() {
    if(op[0] == 'P') {
        if(!m.count(s[sid]))m[s[sid]] = sid, stake[++top] = sid++;
        else stake[++top] = m[s[sid]];
    } else if(op[0] == 'D') {
        int a = stake[top];
        stake[++top] = a;
    } else if(op[0] == 'U') {
        int a = stake[top--];
        int b = stake[top--];
        Set x = s[a];
        for(set<int>::iterator it = s[b].begin(); it!=s[b].end();++it) {
            if(!x.count(*it)) x.insert(*it);
        }
        if(!m.count(x))m[x] = sid, s[sid] = x, stake[++top] = sid++;
        else stake[++top] = m[x];
    } else if(op[0] == 'I') {
        int a = stake[top--];
        int b = stake[top--];
        Set x;
        for(set<int>::iterator it = s[b].begin(); it!=s[b].end();++it) {
            if(s[a].count(*it)) x.insert(*it);
        }
        if(!m.count(x))m[x] = sid, s[sid] = x, stake[++top] = sid++;
        else stake[++top] = m[x];
    } else if(op[0] == 'A') {
        int a = stake[top--];
        int b = stake[top--];
        Set x = s[b];
        x.insert(a);
        if(!m.count(x))m[x] = sid, s[sid] = x, stake[++top] = sid++;
        else stake[++top] = m[x];
    }
}

int main(){
    #ifdef _LOCAL
    IN;
    #endif // _LOCAL

    cin >> t;
    while(t--) {
        top = 0; sid = 0; m.clear();
        scanf("%d", &n);
        for(int i = 0; i < n; ++i) s[i].clear();
        for(int i = 0; i < n; ++i) {
            cin >> op;
            solve();
            cout << s[stake[top]].size() <<endl;
        }
        cout <<"***" <<endl;
    }
    return 0;
}

这是书上的代码

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <map>
#include <stack>

using namespace std;
const int maxn = 2e6 + 5;
const int INF = (1<<31)-1;
#define met(a, b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
typedef long long LL;
typedef set<int> Set;
//STL内置的集合操作
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x,x.begin())

map<Set, int> m;        //把集合映射成ID
vector<Set> S;          //记录下来的set集

//根据集合找下标,没有的话分配新的
int ID(Set x) {
    if(m.count(x)) return m[x];
    S.push_back(x);
    return m[x] = S.size() - 1;
}

stack<int> s;
int n; char op[10];

int main(){
    #ifdef _LOCAL
    IN;
    #endif // _LOCAL
    int t; cin >> t;
    while(t--) {
        cin >> n;
        for(int i = 0; i < n; ++i) {
            cin >> op;
            if(op[0] == 'P') s.push(ID(Set()));
            else if(op[0] == 'D') s.push(s.top());
            else {
                Set x1 = S[s.top()]; s.pop();
                Set x2 = S[s.top()]; s.pop();
                Set x;

                if(op[0] == 'U') set_union(ALL(x1), ALL(x2), INS(x));
                if(op[0] == 'I') set_intersection(ALL(x1), ALL(x2), INS(x));
                if(op[0] == 'A') x = x2, x.insert(ID(x1));
                s.push(ID(x));
            }
            cout << S[s.top()].size() <<endl;
        }
        cout << "***" << endl;
    }

    return 0;
}

其实也没差哪去,只是不如书上的美观而已^0^

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值