题意
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 modern mathematics, in the sense of a theory
invoked to justify assumptions made in mathematics
concerning the existence of mathematical 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 instead 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.
就是一个模拟栈操作、集合操作题目。
想法
看了白书的做法...
首先要用到set<typename>
来模拟集合,那set里装什么呢,第一感觉是装set(根据题意),那这样声明变量时岂不是就要递归了...
这里的做法是使用使用set<int>
,每个set里装整数,每个整数代表不同类型的set集合。
怎么给不同类型的set分配不同的值呢?于是要用map<set<int>,int>
。利用map类型每个键值只对应一个值的特点,不断给新的set分配值。
接下来就是用stack<set<int>>
来模拟栈的操作了。
注意这里map运用的方法很不错。
代码
#include<iostream>
#include<cstdio>
#include<set>
#include<stack>
#include<map>
#include<algorithm>
using namespace std;
typedef set<int> si;
int cases;
int o;
int nowhash;
map<si,int>gethash;
stack<si>sta;
inline void distributeHash(si s){
map<si,int>::iterator it=gethash.find(s);
if(it!=gethash.end())return;
gethash.insert(pair<si,int>(s,nowhash++));
}
void push(){
si ns;
sta.push(ns);
distributeHash(ns);
printf("0\n");
}
void dup(){
sta.push(sta.top());
distributeHash(sta.top());
printf("%d\n",sta.top().size());
}
void add(){
si fir=sta.top();
sta.pop();
si sec=sta.top();
sta.pop();
sec.insert(gethash[fir]);
sta.push(sec);
distributeHash(sec);
printf("%d\n",sec.size());
}
void intersect(){
si fir=sta.top();
sta.pop();
si sec=sta.top();
sta.pop();
si in;
for(si::iterator it=fir.begin();it!=fir.end();it++)
if(sec.count(*it))
in.insert(*it);
sta.push(in);
distributeHash(in);
printf("%d\n",in.size());
}
void unions(){
si fir=sta.top();
sta.pop();
si sec=sta.top();
sta.pop();
for(si::iterator it=fir.begin();it!=fir.end();it++)
sec.insert(*it);
sta.push(sec);
distributeHash(sec);
printf("%d\n",sec.size());
}
inline void init(){
while(!sta.empty())
sta.pop();
gethash.clear();
nowhash=0;
}
int main(){
cin>>cases;
while(cases--){
init();
scanf("%d",&o);
string op;
for(int i=1;i<=o;i++){
cin>>op;
if(op=="PUSH")
push();
else if(op=="DUP")
dup();
else if(op=="ADD")
add();
else if(op=="UNION")
unions();
else if(op=="INTERSECT")
intersect();
}
printf("***\n");
}
return 0;
}