uvaoj-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 of a foundational theory in modern mathematics,
in the sense of a theory invoked to justify assumptions
made in mathematics concerning the existence of mathe-
matical objects (such as numbers or functions) and their
properties. Formal versions of set theory also have a
foundational role to play as specifying a theoretical ideal
of mathematical rigor in proofs.
Given this importance of sets, being the basis of
mathematics, a set of eccentric theorist set off to construct a supercomputer operating on sets in-
stead of numbers. The initial SetStack Alpha is under construction, and they need you to simulate it
in order to verify the operation of the prototype.
The computer operates on a single stack of sets, which is initially empty. After each operation, the
cardinality of the topmost set on the stack is output. The cardinality of a set S is denoted |S| and is the
number of elements in S. The instruction set of the SetStack Alpha is PUSH, DUP, UNION, INTERSECT,
and ADD.
PUSH will push the empty set {} on the stack.
DUP will duplicate the topmost set (pop the stack, and then push that set on the stack twice).
UNION will pop the stack twice and then push the union of the two sets on the stack.
INTERSECT will pop the stack twice and then push the intersection of the two sets on the stack.
ADD will pop the stack twice, add the first set to the second one, and then push the resulting set
on the stack.
For illustration purposes, assume that the topmost element of the stack is
A = {{},{{}}}
and that the next one is
B = {{},{{{}}}}
For these sets, we have |A| = 2 and |B| = 2. Then:
UNION would result in the set {{}, {{}}, {{{}}}}. The output is 3.
INTERSECT would result in the set {{}}. The output is 1.
ADD would result in the set {{}, {{{}}}, {{},{{}}}}. The output is 3.

Input
An integer 0 T 5 on the first line gives the cardinality of the set of test cases. The first line of each
test case contains the number of operations 0 N 2000. Then follow N lines each containing one of
the five commands. It is guaranteed that the SetStack computer can execute all the commands in the
sequence without ever popping an empty stack.
Output
For each operation specified in the input, there will be one line of output consisting of a single integer.
This integer is the cardinality of the topmost element of the stack after the corresponding command
has executed. After each test case there will be a line with *** (three asterisks).


Sample Input
2
9
PUSH
DUP
ADD
PUSH
ADD
DUP
ADD
DUP
UNION

5
PUSH
PUSH

ADD
PUSH
INTERSECT


Sample Output
0
0
1
0
1
1
2
2
2
***
0
0
1
0
0
***


题解:刘汝佳白书115页例题5-5;考察map和stack和vector等一大堆STL容器的运用;

这道题和接下来的5-6有些类似,比较嘲讽的是,这道题比下一道简单,然而我却先做出来了下一道,这就证明了勇气的重要性,只要敢做,就没有什么真的能难得倒你的;

插入5-6(uvaoj540)博客链接:团队列队(team queue)

//

这道题首先做之前要明白,集合SET作为一种数据类型,可以在不定长数组vector中定义,它在某种程度上可以看作int之类的变量,这样看着,题目就变得清晰了;

利用vector<set<int> > 来存放集合们;

利用map将集合和所设置的标记一一对应;

定义stack<int> 存放的就是这些标记,在运算过程中可以利用标记在vector中提取set进行运算;

具体代码如下,在代码上会进行详细注释,在文章末尾会做学习笔记:


code:

/**************
options list:
PUSH,DUP,UNION,INTERSECT,ADD;
**************/
#include <stack>
#include <map>
#include <vector>
#include <string>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
#define ALL(x) x.begin(),x.end()//利用宏定义将字符串替换,也就是替换名称;
#define INS(x) inserter(x,x.begin())

vector <set<int> > setcache;//存放set的数组setcache,(cache中文释“藏物处”);
map<set<int>,int> idcache;//利用map来给每一个集合设定标记;


int id(set<int> ans)//集合和标记的转化加工处;
{
    if(idcache.count(ans)==0)//判断所加入的集合是否已经存在,不存在则给它创建一个新标记;
    {
        setcache.push_back(ans);
        idcache[ans]=setcache.size()-1;//标记是从零开始的,而大小是从一开始的,所以要减一;
        return idcache[ans];
    }
    else
    {
        return idcache[ans];
    }
}
int main()
{
    stack <int> s;
    idcache.clear();
    int n,T;
    string order;
    cin>>T;
    while(T--)
    {
        cin>>n;
        while(n--)
        {
        cin>>order;
        if(order[0]=='P')
        {
            s.push(id(set<int> ()));//在s中push进去的实际上是空集的标记;
        }
        else if(order[0]=='D')
        {
            s.push(s.top());//将标记复制i再push一次;
        }
        else
        {
            set<int> xa;
            xa=setcache[s.top()];
            s.pop();
            set<int> xb;
            xb=setcache[s.top()];
            s.pop();
            set<int> x;//else下边到这里位置的内容都是将stack中最后进的两个集合分别提取,分别用xa,xb来保存,x是运算后的集合;
            if(order[0]=='U')
            {
                set_union(ALL(xa),ALL(xb),INS(x));//存放于algorithm中的函数,用于集合的运算,下面的笔记中会有记录;
            }
            if(order[0]=='I')
            {
                set_intersection(ALL(xa),ALL(xb),INS(x));
            }
            if(order[0]=='A')
            {
                x=xb;
                x.insert(id(xa));
            }
            s.push(id(x));
        }
        cout<<setcache[s.top()].size()<<endl;
        }
        cout<<"***\n";
    }
    return 0;
}


笔记:
这道题最重要的思想就是利用地址来提取集合,也就是用map将两种类型的元素连接起来,一一映射,这也许和数据结构的树形结构有所关联,看来别人说的学算法先学数据结构是有很大道理的;
题目中有一些小技巧值得学习:
1.#define 宏定义;
宏定义替换的是字符串,常见的是用ll 替换long long ,这种替换不用也罢,但是这道题里面的替换很有必要,因为需要替换的内容重复使用并且较复杂;
2.有关于集合的函数:
题目中出现的只有
set_union(),取并集;
set_intersection(),取交集;
上边两个函数中的前四个指针分别为两个集合开始和结束的位置,最后两个元素则为输出的集合名称和答案集合指针;

//PS:难得的一次ac,虽然是对着书慢慢敲的,起步晚就要走的比别人快,努力!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值