UVa12096 - The SetStack Computer

來源:互聯網
上載者:User

標籤:

The computer op erates on a single stack of sets, which is initially empty. After each op eration, 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
***

 

 1 //By LYLtim 2 //2015.4.24 3  4 #include <iostream> 5 #include <set> 6 #include <stack> 7 #include <map> 8 #include <vector> 9 #include <algorithm>10 11 using namespace std;12 13 using Set = set<int>;14 map<Set, int> IDCache;15 vector<Set> SetCache;16 17 int ID (Set set) {18     if (IDCache.count(set))19         return IDCache[set];20     SetCache.push_back(set);21     return IDCache[set] = SetCache.size() - 1;22 }23 24 int main() {25     int t, n;26     stack<int> s;27     cin >> t;28     for (int i = 0; i < t; i++) {29         cin >> n;30         for (int j = 0; j < n; j++) {31             string op;32             cin >> op;33             if (op[0] == ‘P‘)34                 s.push(ID(Set()));35             else if (op[0] == ‘D‘)36                 s.push(s.top());37             else {38                 Set s1 = SetCache[s.top()]; s.pop();39                 Set s2 = SetCache[s.top()]; s.pop();40                 Set tmpSet;41                 if (op[0] == ‘U‘) {42                     set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(tmpSet, tmpSet.begin()));43                     s.push(ID(tmpSet));44                 }45                 else if (op[0] == ‘I‘) {46                     set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(tmpSet, tmpSet.begin()));47                     s.push(ID(tmpSet));48                 }49                 else if (op[0] == ‘A‘){50                     s2.insert(ID(s1));51                     s.push(ID(s2));52                 }53             }54             cout << SetCache[s.top()].size() << endl;55         }56         cout << "***" << endl;57     }58 }

 

UVa12096 - The SetStack Computer

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.