码迷,mamicode.com
首页 > 其他好文 > 详细

12096 - The SetStack Computer UVA

时间:2015-01-24 22:36:39      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

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 specied 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 }

 

12096 - The SetStack Computer UVA

标签:

原文地址:http://www.cnblogs.com/767355675hutaishi/p/4246654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!