12096 - The SetStack Computer UVA

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 mathemat-
ics concerning the existence of mathematical objects
(such as numbers or functions) and their properties.
Formal versions of set theory also have a founda-
tional 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
j
S
j
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
fg
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 rst 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
=
ffg
;
ffggg
and that the next one is
B
=
ffg
;
fffgggg
For these sets, we have
j
A
j
= 2 and
j
B
j
= 2. Then:

UNION
would result in the set
ffg
,
ffgg
,
fffgggg
. The output is 3.

INTERSECT
would result in the set
ffgg
. The output is 1.

ADD
would result in the set
ffg
,
fffggg
,
ffg
,
ffgggg
. The output is 3.
Input
An integer 0

T

5 on the rst line gives the cardinality of the set of test cases. The rst line of each
test case contains the number of operations 0

N

2000. Then follow
N
lines each containing one of
the ve 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 specied 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).
SampleInput
2
9
PUSH
DUP
ADD
PUSH
ADD
DUP
ADD
DUP
UNION
5
PUSH
PUSH
ADD
PUSH
INTERSECT
SampleOutput
0
0
1
0
1
1
2
2
2
***
0
0
1
0
0
***
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <cmath>
  6 #include <string>
  7 #include <vector>
  8 #include <set>
  9 #include <map>
 10 #include <queue>
 11 #include <stack>
 12 #include <sstream>
 13 #include <cctype>
 14 using namespace std;
 15 const int INF = 0x7fffffff;
 16 const double EXP = 1e-8;
 17 const int MS = 105;
 18 typedef long long LL;
 19 int id;
 20 typedef set<int> SET;
 21 map<SET, int>  mp;
 22 typedef set<int>::iterator IT;
 23 stack<SET> sta;
 24 void ID(SET s)
 25 {
 26     if (mp.count(s))
 27         return;
 28     mp[s] = id++;
 29 }
 30
 31 void PUSH()
 32 {
 33     SET S;
 34     ID(S);
 35     sta.push(S);
 36 }
 37
 38 void DUP()
 39 {
 40     sta.push(sta.top());
 41 }
 42
 43 void UNION()
 44 {
 45     SET S, S2;
 46     S2 = sta.top();
 47     sta.pop();
 48     S = sta.top();
 49     sta.pop();
 50     for (IT it = S2.begin(); it != S2.end(); it++)
 51         S.insert(*it);
 52     ID(S);
 53     sta.push(S);
 54 }
 55
 56 void INTERSECT()
 57 {
 58     SET S, S2, S3;
 59     S2 = sta.top();
 60     sta.pop();
 61     S3 = sta.top();
 62     sta.pop();
 63     for (IT it = S2.begin(); it != S2.end(); it++)
 64     {
 65         if (S3.count(*it))
 66             S.insert(*it);
 67     }
 68     ID(S);
 69     sta.push(S);
 70 }
 71
 72 void ADD()
 73 {
 74     SET S1, S2;
 75     S1 = sta.top();
 76     sta.pop();
 77     S2 = sta.top();
 78     sta.pop();
 79     S2.insert(mp[S1]);
 80     ID(S2);
 81     sta.push(S2);
 82 }
 83
 84 void TOPSIZE()
 85 {
 86     cout << sta.top().size() << endl;
 87 }
 88 void solve()
 89 {
 90     char op[10];
 91     cin >> op;
 92     switch (op[0])
 93     {
 94     case ‘P‘:PUSH(); break;
 95     case ‘D‘:DUP(); break;
 96     case ‘U‘:UNION(); break;
 97     case ‘I‘:INTERSECT(); break;
 98     case ‘A‘:ADD(); break;
 99     }
100     TOPSIZE();
101 }
102 int main()
103 {
104     int T;
105     cin >> T;
106     while (T--)
107     {
108         int n;
109         cin >> n;
110         mp.clear();
111         while (!sta.empty())
112             sta.pop();
113         id = 0;
114         while (n--)
115             solve();
116         cout << "***" << endl;
117     }
118     return 0;
119 }
时间: 2024-10-18 07:00:01

12096 - The SetStack Computer UVA的相关文章

uva 12096 - The SetStack Computer(STL)

UVA 12096 - The SetStack Computer 题目链接 题意:几个操作,push是在栈顶加入一个空集,dup是复制栈顶集合,在放入栈顶,union是把头两个取并集放回,int是头两个取交集放回,add是取头两个,把第一个当成一个集合加入第二个,每次操作输出栈顶集合的里面的个数 思路:用set,stack模拟,然后利用map去hash一个集合,模拟即可 代码: #include <cstdio> #include <cstring> #include <s

UVA - 12096 The SetStack Computer(编码,STL)

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 o

UVAOJ 12096 The SetStack Computer(STL的运用)

12096 The SetStack Computer Background from Wikipedia: Set theory is a branch ofmathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play therole of

UVa 12096 The SetStack Computer

Description 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

12096 - The SetStack Computer

The SetStack Computer PS:因为该题排版较麻烦,这里给出OJ网址:UVa12096 - The SetStack Computer 有一个专门为了集合运算而设计的"集合栈"计算机.该机器有一个初始为空的栈,并且支持以下操作. PUSH:空集"{}"入栈. DUP:把当前栈顶元素复制一份后再入栈. UNION:出栈两个集合,然后把二者的并集入栈. INTERSECT:出栈两个集合,然后把二者的交集入栈. ADD:出栈两个集合,然后把先出栈的集合加

uva 12096 The SetStack Computer(STL set)

题意: 有5种操作: PUSH:加入"{}"空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出栈的加入到后出栈的集合中. 输入不超过2000, 保证操作顺利进行. 分析: 用set<int>(本身又可以映射成一个int)去模拟集合,所以可以将不同的集合映射成int型. 用一个Map<set<int>,int> 去映射成不同的int. 以后需要se

uva 12096 - The SetStack Computer(STL 的运用)

这道题目貌似就是在不停地用STL中的内容,对STL熟练运用的大神估计坐起来会比较easy.. 不过对于我这种看着代码还是需要上网查函数运用的菜鸟来说,若让我自己做这道题,肯定不会使用STL.. 就当对STL的学习了. #include<cstdio> #include<iostream> #include<cstring> #include<string> #include<set> #include<stack> #include&

UVa - 12096 The SetStack Computer(STL容器综合,强推!)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42064 #include <iostream> #include <algorithm> #include <string> #include <map> #include <set> #include <vector> #include <stack> #define ALL(x) x.b

The SetStack Computer UVA - 12096

题意:初始状态的栈内包含一个空集,对栈进行一下操作: PUSH:向栈内压入一个空集 DUP:复制栈顶,并压入栈内 UNION:将栈顶端两个集合出栈,并将两个元素的并集入栈 INTERSECT:将栈顶端两个集合出栈,并将两个元素的交集入栈 ADD:将栈顶端两个集合出栈,将先出栈元素加入后出栈元素的集合中,而后入栈 对于每次操作,输出栈顶集合的元素数. 思路:声明一个栈st用来存放集合,声明多个堆用于表示集合,声明映射<set,int>表示集合编号,对于每次待入栈的集合进行编号查找工作,若已经有编