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())
map<set<int>,int> idcache;//利用map来给每一个集合设定标记;
{
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;
}