UVa12096.The SetStack Computer

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3248

13916058 12096 The SetStack Computer Accepted C++ 0.302 2014-07-21 03:43:15

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 mathemat ics concerning the existence of mathematical objects
(such as numbers or functions) and their properties.UVa12096.The SetStack Computer
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 unde
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 jSj 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 se
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 jAj = 2 and jBj = 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 o
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).
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
***


解题思路:模拟五个操作即可。读题十分重要。还有通过此题,可以增加使用set容器的熟练程度。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int MAXN = ;
const int N = ; int cnt;
stack<set<int> > stk;
map<set<int>, int> mp;
set<int> s1, s2; void pop() {
s1 = stk.top();
stk.pop();
s2 = stk.top();
stk.pop();
} void Push() {
set<int> s;
stk.push(s);
printf("0\n");
} void Dup() {
set<int> s;
s = stk.top();
stk.push(s);
printf("%d\n", s.size());
} void Union() {
pop();
set<int>::iterator it;
for (it = s1.begin(); it != s1.end(); it++)
s2.insert(*it);
stk.push(s2);
printf("%d\n", s2.size());
} void Intersect() {
pop();
set<int> s3;
set<int>::iterator it;
for (it = s1.begin(); it != s1.end(); it++)
if (s2.find(*it) != s2.end())
s3.insert(*it);
stk.push(s3);
printf("%d\n", s3.size());
} void Add() {
pop();
if (s1.empty())
s2.insert();
else {
if (!mp[s1])
mp[s1] = cnt++;
s2.insert(mp[s1]);
}
stk.push(s2);
printf("%d\n", s2.size());
} int main() {
int t, n;
string str;
cin >> t;
while ( t --) {
cin >> n;
while (!stk.empty())
stk.pop();
cnt = MAXN;
mp.clear();
while (n--) {
cin >> str;
if (str[] == 'P')
Push();
else if (str[] == 'D')
Dup();
else if (str[] == 'U')
Union();
else if (str[] == 'I')
Intersect();
else Add();
}
printf("***\n");
}
return ;
}
上一篇:剑指Offer:面试题20——顺时针打印矩阵(java实现)


下一篇:C++ mem_fun 和 mem_fun_ref 的用法