UVa 12096 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 inmathematics concerning the existence of mathematical objects (such as numbers or functions) and their properties. Formal versions of set theory also have a foundational 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 Set-Stack 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 |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 ≤ 2 000. 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
***
分析

set的使用

方法

编辑

begin() 返回指向第一个元素的迭代器

clear() 清除所有元素

count() 返回某个值元素的个数

empty() 如果集合为空,返回true(真)

end() 返回指向最后一个元素之后的迭代器,不是最后一个元素

equal_range() 返回集合中与给定值相等的上下限的两个迭代器

erase() 删除集合中的元素

find() 返回一个指向被查找到元素的迭代器,如果没找到则返回end()

get_allocator() 返回集合的分配器

insert() 在集合中插入元素

lower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器

key_comp() 返回一个用于元素间值比较的函数

max_size() 返回集合能容纳的元素的最大限值

rbegin() 返回指向集合中最后一个元素的反向迭代器

rend() 返回指向集合中第一个元素的反向迭代器

size() 集合中元素的数目

swap() 交换两个集合变量

upper_bound() 返回大于某个值元素的迭代器

value_comp() 返回一个用于比较元素间的值的函数

集合操作

编辑

std::set_intersection() :这个函数是求两个集合的交集。

std::set_union() :求两个集合的并集

std::set_difference():差集

std::set_symmetric_difference():得到的结果是 第一个迭代器相对于第二个的差集 并上

第二个相对于第一个的差集
 
对函数和定义的一些理解:
函数和定义的一些理解
 
主要还是stl的理解。。
 

代码:

 1 #include<iostream>
 2 #include<set>
 3 #include<algorithm>
 4 #include<map>
 5 #include<vector>
 6 #include<stack>
 7 using namespace std;
 8 typedef set<int>se;
 9 map<se,int>idcache;
10 vector<se>setcache;
11 
12 
13 int id(se x);
14 #define all(x) x.begin(),x.end()
15 #define ins(x) inserter(x,x.begin())//宏的定义可以理解为 类似于函数 
16 
17 //查找给定集合x的id,找不到分配新的id 
18 int id(se x)
19 {
20     if(idcache.count(x))return idcache[x];
21     setcache.push_back(x);
22     return idcache[x]=setcache.size()-1;
23 }
24 int main()
25 {
26     stack<int>s;
27     int m;
28     cin>>m;
29     while(m--)
30     {
31         
32     int n;
33     cin>>n;
34     for(int i=0;i<n;i++)
35     {
36         string op;
37         cin>>op;
38         if(op[0]=='P')
39         {
40          s.push(id(se()));
41           // cout<<s.top()<<endl;    
42         }
43         else if(op[0]=='D')s.push(s.top());
44         else{
45             se x1=setcache[s.top()];s.pop();
46             se x2=setcache[s.top()];s.pop();
47             se x;
48             if(op[0]=='U')set_union (all(x1),all(x2),ins(x));
49             if(op[0]=='I')set_intersection(all(x1),all(x2),ins(x));
50             if(op[0]=='A'){x=x2;x.insert(id(x1));}
51             s.push(id(x));
52             
53         }
54         //cout<<s.top()<<endl;
55         cout<<setcache[s.top()].size()<<endl;    
56     }
57     cout<<"***"<<endl; 
58     }
59     return 0;
60  } 

 



posted @ 2017-03-29 16:59  masking_timeflows  阅读(261)  评论(0)    收藏  举报