一个FLAG #14# The SetStack Computer

集合栈计算机,完整题目见参考[1]

书上的原始代码如下:

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <algorithm>
// algorithm是必要的?否则 set_union报错 

using namespace std;

typedef set<int> Set;
map<Set, int> ID_cache;
vector<Set> Set_cache;

// 查找指定集合的 id 如果找不到,分配一个新的
int ID(Set x) {
    if (ID_cache.count(x)) {
        return ID_cache[x]; // 返回相应 ID
    } else {
        Set_cache.push_back(x); // 把集合放入集合cache中
        return ID_cache[x] = Set_cache.size() - 1;
        // 新分配ID也就是新集合在集合cache中的位置
        // Set_cache[id] 就是那个对应的集合
    }
}

#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())

int main()
{
    stack<int> s; // 方便起见,为每个不同的集合,分配一个唯一的ID 

    int n;
    cin >> n;
    for (int i = 0; i != n; ++i) {
        string op;
        cin >> op;
        if (op[0] == ‘P‘) {
            s.push(ID(Set()));
            // PUSH操作 空集{}入栈
            // 这里实际是创建了一个空的 set<int> 对象
            // 然后将其添加到 set_cache中 并分配了一个唯一的id
            // 通过 set_cache[id] 可以拿到这个集合,通过 IDcache[这个集合] 可以拿到它的id
            // 通过ID()函数封装了这些操作。然后用id来充当集合进行操作
        } else if (op[0] == ‘D‘) {
            s.push(s.top());
            // DUP操作
            // 只是把一个数字push进去。集合本身没有拷贝
            // DUP多次的结果就是,栈中会有多个数字指向一个集合
        } else {
            // UNION 以及 INTERSECT 以及 ADD操作首先都需要出栈两个集合
            Set x1 = Set_cache[s.top()]; // setcache中的集合是永不消除的
            s.pop();
            Set x2 = Set_cache[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));
                // 所以这个集合也是插入数字 并没有真正把集合插入到集合里去
                // 所以是用 set<int> 就可以
            }
            s.push(ID(x));
        }
        cout << Set_cache[s.top()].size() << endl;
    }
}

改了一下变量名,感觉还是没有很清晰。

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <algorithm>

using namespace std;

typedef set<int> MySet;
map<MySet, int> ids;
vector<MySet> sets;  

int getId(MySet mySet) {
    if (ids.count(mySet)) {
        return ids[mySet];
    } else {
        sets.push_back(mySet);
        return ids[mySet] = sets.size() - 1;
    }
}

MySet getMySet(int id) {
    return sets[id];
}

#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())

int main()
{
    stack<int> stack;

    int n;
    cin >> n;
    for (int i = 0; i != n; ++i) {
        string op;
        cin >> op;
        if (op[0] == ‘P‘) {
            stack.push(getId(MySet()));
        } else if (op[0] == ‘D‘) {
            stack.push(stack.top());
        } else {
            MySet set1 = getMySet(stack.top());
            stack.pop();
            MySet set2 = getMySet(stack.top());
            stack.pop();
            MySet set;
            if (op[0] == ‘U‘) {
                set_union(ALL(set1), ALL(set2), INS(set));
            }
            if (op[0] == ‘I‘) {
                set_intersection(ALL(set1), ALL(set2), INS(set));
            }
            if (op[0] == ‘A‘) {
                set = set2;
                set.insert(getId(set1));
            }
            stack.push(getId(set));
        }
        cout << getMySet(stack.top()).size() << endl;
    }
}

参考

[1] https://vjudge.net/problem/UVA-12096

原文地址:https://www.cnblogs.com/xkxf/p/12680031.html

时间: 2024-08-27 11:01:50

一个FLAG #14# The SetStack Computer的相关文章

UVAOJ 12096 The SetStack Computer(STL的运用)

12096 The SetStack Computer Background from Wikipedia: Set theory is a branch ofmathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play therole of

UVa 12096 The SetStack Computer

Description Background from Wikipedia: "Set theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational

12096 - The SetStack Computer UVA

Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational theory in m

uva 12096 - The SetStack Computer(STL)

UVA 12096 - The SetStack Computer 题目链接 题意:几个操作,push是在栈顶加入一个空集,dup是复制栈顶集合,在放入栈顶,union是把头两个取并集放回,int是头两个取交集放回,add是取头两个,把第一个当成一个集合加入第二个,每次操作输出栈顶集合的里面的个数 思路:用set,stack模拟,然后利用map去hash一个集合,模拟即可 代码: #include <cstdio> #include <cstring> #include <s

UVa12096 - The SetStack Computer

The computer op erates on a single stack of sets, which is initially empty. After each op eration, the cardinality of the topmost set on the stack is output. The cardinality of a set S is denoted | S | and is thenumber of elements in S. The instructi

UVA - 12096 The SetStack Computer(编码,STL)

12096 The SetStack Computer Background from Wikipedia: "Set theory is a branch of mathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century.Initially controversial, set theory has come to play the role o

UVA12096 - The SetStack Computer(set + map映射)

UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来,在入栈两次. add : 出栈两次,把第一个集合作为一个元素放入第二个集合中,再将第二个集合入栈 union: 出栈两次,取这两个集合的并集,将结果入栈. intersect: 出栈两次,取这两个集合的交集,将结果入栈. 每次执行动作后还需要输出目前栈顶集合的元素个数. 解题思路:这题可以用栈和se

12096 - The SetStack Computer

The SetStack Computer PS:因为该题排版较麻烦,这里给出OJ网址:UVa12096 - The SetStack Computer 有一个专门为了集合运算而设计的"集合栈"计算机.该机器有一个初始为空的栈,并且支持以下操作. PUSH:空集"{}"入栈. DUP:把当前栈顶元素复制一份后再入栈. UNION:出栈两个集合,然后把二者的并集入栈. INTERSECT:出栈两个集合,然后把二者的交集入栈. ADD:出栈两个集合,然后把先出栈的集合加

UVa 12096 (STL) The SetStack Computer

题意: 有一个集合栈计算机,栈中的元素全部是集合,还有一些相关的操作.输出每次操作后栈顶集合元素的个数. 分析: 这个题感觉有点抽象,集合还能套集合,倒是和题中配的套娃那个图很贴切. 把集合映射成ID,就可以用 stack<int>来模拟题中的集合栈,然后用 vector<Set> 来根据下标进行集合的索引. 代码虽短,但还须多体会. 1 #include <cstdio> 2 #include <string> 3 #include <vector&