UVa12096.The SetStack Computer

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   

題目連結: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.
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容器的熟練程度。

 

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <stack> 5 #include <map> 6 #include <set> 7 using namespace std; 8 const int MAXN = 2010; 9 const int N = 20;10 11 int cnt;12 stack<set<int> > stk;13 map<set<int>, int> mp;14 set<int> s1, s2;15 16 void pop() {17     s1 = stk.top();18     stk.pop();19     s2 = stk.top();20     stk.pop();21 }22 23 void Push() {24     set<int> s;25     stk.push(s);26     printf("0\n");27 }28 29 void Dup() {30     set<int> s;31     s = stk.top();32     stk.push(s);33     printf("%d\n", s.size());34 }35 36 void Union() {37     pop();38     set<int>::iterator it;39     for (it = s1.begin(); it != s1.end(); it++)40         s2.insert(*it);41     stk.push(s2);42     printf("%d\n", s2.size());43 }44 45 void Intersect() {46     pop();47     set<int> s3;48     set<int>::iterator it;49     for (it = s1.begin(); it != s1.end(); it++)50         if (s2.find(*it) != s2.end())51             s3.insert(*it);52     stk.push(s3);53     printf("%d\n", s3.size());54 }55 56 void Add() {57     pop();58     if (s1.empty())59         s2.insert(0);60     else {61         if (!mp[s1])62             mp[s1] = cnt++;63         s2.insert(mp[s1]);64     }65     stk.push(s2);66     printf("%d\n", s2.size());67 }68 69 int main() {70     int t, n;71     string str;72     cin >> t;73     while ( t --) {74         cin >> n;75         while (!stk.empty())76             stk.pop();77         cnt = MAXN;78         mp.clear();79         while (n--) {80             cin >> str;81             if (str[0] == ‘P‘)82                 Push();83             else if (str[0] == ‘D‘)84                 Dup();85             else if (str[0] == ‘U‘)86                 Union();87             else if (str[0] == ‘I‘)88                 Intersect();89             else Add();90         }91         printf("***\n");92     }93     return 0;94 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.