您的位置:首页 > 其它

uva 12096 The SetStack Computer

2017-05-24 15:45 387 查看
原题:

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.

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

(三个星号)

中文:

给你一个栈,让你每次操作输出栈顶元素中的个数。操作包括push表示添加一个空集合,dup表示复制栈顶元素后入栈,union求出栈的两个集合的并,然后入栈,intersect求交入栈,add表示先出栈的集合加入后出栈的集合后入栈。

#include <bits/stdc++.h>
using namespace std;
typedef set<int> Set;
map<Set,int> id;
vector<Set> vs;
stack<int> st;
int ID(const Set &x)
{
if(id.find(x)!=id.end())
return id[x];
vs.push_back(x);
return id[x]=vs.size()-1;
}
int main ()
{
ios::sync_with_stdio(false);
int t,n;
cin>>t;
while(t--)
{
vs.clear();
id.clear();
while(!st.empty())
st.pop();
cin>>n;
string s;
for(int i=0;i<n;i++)
{
cin>>s;
if(s=="PUSH")
{
st.push(ID(Set()));
}
else
{
if(s=="DUP")
{
st.push(st.top());
}
else
{
Set s1=vs[st.top()];
st.pop();
Set s2=vs[st.top()];
st.pop();
Set tmp;
if(s=="UNION")
{
set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(tmp,tmp.begin()));
}
if(s=="INTERSECT")
{
set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(tmp,tmp.begin()));
}
if(s=="ADD")
{
tmp=s2;
tmp.insert(ID(s1));
}
st.push(ID(tmp));
}
}

cout<<vs[st.top()].size()<<endl;
}
cout<<"***"<<endl;
}
return 0;
}


解答:

紫书上面的例题,练习使用stl的不错选择。最主要的思路就是用stl中的set和一个整数进行映射。每个set当中存储的整数为不同set的标识,通过这个标识可以找到对应的set。用这种方法就可以实现集合与集合之间的包含关系了。例如一个集合set1当中包含的所有标识表示在这个集合当中,里面的元素是其中标识对应的集合。

最后利用书中给出的方法使用映射id的方法来操作即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: