uva 12096 The SetStack Computer(STL set)

题意:

有5种操作:

PUSH:加入“{}”空集合入栈。

DUP:栈顶元素再入栈。

UNION:出栈两个集合,取并集入栈。

INTERSECT:出栈两个集合,取交集入栈。

ADD:出栈两个集合,将先出栈的加入到后出栈的集合中。

输入不超过2000, 保证操作顺利进行。

分析:

用set<int>(本身又可以映射成一个int)去模拟集合,所以可以将不同的集合映射成int型。

用一个Map<set<int>,int> 去映射成不同的int。

以后需要set做交集并集的时候再从map中拿出set做操作再映射,有点复杂,需要慢慢体会这种映射的方法。

代码有两个难点:

1.插入迭代器:

2.set_union() , set_intersection.

(待补)

代码参考刘汝佳第五章例题

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <set>
 5 #include <vector>
 6 #include <string>
 7 #include <map>
 8 #include <queue>
 9 #include <stack>
10 #include <algorithm>
11 #include <sstream>
12
13 #define ALL(x) x.begin(),x.end()
14 #define INS(s) inserter(x,x.begin())
15 using namespace std;
16 typedef set<int> Set;
17 map<Set, int> IDcache;
18 vector<Set> Setcache;
19 int ID (Set x)
20 {
21     if(IDcache.count(x)) return IDcache[x];
22     Setcache.push_back(x);
23     return IDcache[x] = Setcache.size() - 1; //都是空集, 唯一区别就是大小
24 }
25 int main()
26 {
27 //    freopen("1.txt","r",stdin);
28     int t;
29     cin>>t;
30     while(t--)
31     {
32         IDcache.clear();
33         Setcache.clear();
34         stack<int> s;
35         int n;
36         cin>>n;
37         for(int i = 0 ; i < n ; i++)
38         {
39             string op;
40             cin >> op;
41             if(op[0] == ‘P‘)
42                s.push(ID(Set()));
43             else if(op[0] == ‘D‘)
44                 s.push(s.top());
45             else
46             {
47                 Set x1 = Setcache[s.top()];
48                 s.pop();
49                 Set x2 = Setcache[s.top()];
50                 s.pop();
51                 Set x;
52                 if(op[0] == ‘U‘) set_union(ALL(x1),ALL(x2),INS(x));
53                 if(op[0] == ‘I‘) set_intersection(ALL(x1), ALL(x2), INS(x));
54                 if(op[0] == ‘A‘)
55                 {
56                     x=x2;
57                     x.insert(ID(x1));
58                 }
59                 s.push(ID(x));
60             }
61              cout<< Setcache[s.top()].size() <<endl;
62         }
63         cout<<"***"<<endl;
64     }
65 }
时间: 2024-10-27 19:59:20

uva 12096 The SetStack Computer(STL set)的相关文章

uva 12096 - The SetStack Computer(STL 的运用)

这道题目貌似就是在不停地用STL中的内容,对STL熟练运用的大神估计坐起来会比较easy.. 不过对于我这种看着代码还是需要上网查函数运用的菜鸟来说,若让我自己做这道题,肯定不会使用STL.. 就当对STL的学习了. #include<cstdio> #include<iostream> #include<cstring> #include<string> #include<set> #include<stack> #include&

UVA - 12096 The SetStack Computer(编码,STL)

12096 The SetStack Computer Background from Wikipedia: "Set theory is a branch of mathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century.Initially controversial, set theory has come to play the role o

UVAOJ 12096 The SetStack Computer(STL的运用)

12096 The SetStack Computer Background from Wikipedia: Set theory is a branch ofmathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play therole of

uva 12096 - The SetStack Computer(STL)

UVA 12096 - The SetStack Computer 题目链接 题意:几个操作,push是在栈顶加入一个空集,dup是复制栈顶集合,在放入栈顶,union是把头两个取并集放回,int是头两个取交集放回,add是取头两个,把第一个当成一个集合加入第二个,每次操作输出栈顶集合的里面的个数 思路:用set,stack模拟,然后利用map去hash一个集合,模拟即可 代码: #include <cstdio> #include <cstring> #include <s

UVa 12096 The SetStack Computer

Description 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

UVa - 12096 The SetStack Computer(STL容器综合,强推!)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42064 #include <iostream> #include <algorithm> #include <string> #include <map> #include <set> #include <vector> #include <stack> #define ALL(x) x.b

UVa - 12096 集合栈计算机(STL)

[题意] 有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始为空的栈,并且支持以下操作:PUSH:空集“{}”入栈DUP:把当前栈顶元素复制一份后再入栈UNION:出栈两个集合,然后把两者的并集入栈,并输出并集的sizeINTERSECT:出栈两个集合,然后把二者的交集入栈,并输出交集的sizeADD:出栈两个集合,然后把先出栈的集合加入到后出栈的集合中,把结果入栈,并输出结果的size       每次操作后,输出栈顶集合的大小(即元素个数).例如栈顶元素是A={ {}, {{}

UVA 10474:Where is the Marble?(STL初步)

 Where is the Marble?  Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them. Then Mee

UVA 657-The die is cast(双重BFS)

InterGames is a high-tech startup company that specializes in developing technology that allows users to play games over the Internet. A market analysis has alerted them to the fact that games of chance are pretty popular among their potential custom