The SetStack Computer
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
• 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
***
此题难点在于抽象集合的具体化,如果这一点想不到的话题目的思考就无法进行下去,另外一个难点就是集合与整数的映射,通过这道题,又自我摸索了一个小技巧,就是变量名一定要定义的自己能够理解,如果变量名没法设定的很形象的话,可以在后面加上一定的注释来提醒自己,这样可以保证整个编程过程中思路的清晰。另外操作的模块化可以减少很大的思维量,提高调试的效率。
Liu的做法显然更简洁,由于我第一次接触STL所以不知道set还有set_union和set_intersection操作,学习了。
附代码如下:
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
typedef set<int> SET;
vector<SET> Set;//该向量保存所有可能出现的集合
map<SET,int> SetID;//该映射保存所有集合的下标(ID)
stack<int> SetStack;//该栈按集合的下标(ID)保存
int total,n;
void CLEAR(){
while(SetStack.size())SetStack.pop();
Set.clear();
SetID.clear();
SET x;
x.clear();
Set.push_back(x);
SetID[x]=0;
}
void PUSH(){
SetStack.push(0);
}
void DUP(){
int A=SetStack.top();
SetStack.push(A);
}
void UNION(){
int A=SetStack.top();
SetStack.pop();
int B=SetStack.top();
SetStack.pop();
SET C(Set[B]);
for(SET::iterator it=Set[A].begin();it!=Set[A].end();it++){
C.insert(*it);
}
if(!SetID.count(C)){
SetID[C]=Set.size();
Set.push_back(C);
}
SetStack.push(SetID[C]);
}
void INTERSECT(){
int A=SetStack.top();
SetStack.pop();
int B=SetStack.top();
SetStack.pop();
SET C;
for(SET::iterator it=Set[A].begin();it!=Set[A].end();it++){
if(Set[B].count(*it))C.insert(*it);
}
if(!SetID.count(C)){
SetID[C]=Set.size();
Set.push_back(C);
}
SetStack.push(SetID[C]);
}
void ADD(){
int A=SetStack.top();
SetStack.pop();
int B=SetStack.top();
SetStack.pop();
SET C(Set[B]);
C.insert(A);
if(!SetID.count(C)){
SetID[C]=Set.size();
Set.push_back(C);
}
SetStack.push(SetID[C]);
}
int COUNT(){
int A=SetStack.top();
return Set[A].size();
}
int main(){
cin>>total;
while(total--){
CLEAR();
cin>>n;
string str;
while(n--){
cin>>str;
if(str[0]=='P')PUSH();
if(str[0]=='D')DUP();
if(str[0]=='U')UNION();
if(str[0]=='I')INTERSECT();
if(str[0]=='A')ADD();
cout<<COUNT()<<endl;
}
cout<<"***"<<endl;
}
return 0;
}