UVA 12096 STL map set 的使用

set这个容器也是STL库的一员,并且在algorithm内直接有 set_union set_intersection  这样求并集交集的操作

map 最方便的地方就是 支持下标访问

举例说明 :

 1 #include<iostream>
 2 include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<map>
 6 #include<set>
 7 #include<algorithm>
 8 #include<string>
 9
10 using namespace std;
11
12 int main(void)
13 {
14     map<string,int> smap;
15     smap["Hello"]=3;
16     smap["World"]=1;
17     string ss;
18     cin>>ss;
19     cout<<smap[ss];
20     return 0;
21 }

上面的是 map的用法 特殊的地方只有 15 16 行       而且很好理解 ,不做解释了

map<string,int> 简单理解指的就是  把 string作为下标 数组内的元素为int

实际上是建立起 key-value 的一个对应关系  0.0

下面是题目pdf :

uva.onlinejudge.org/external/120/12096.pdf

本题的思想就是 给每个集合一个独特的ID

同一个集合共享一个ID

代码如下

 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #include<vector>
 5 #include<set>
 6 #include<stack>
 7 #include<algorithm>
 8 #include<map>
 9
10 #define ALL(x) x.begin(),x.end()
11 #define INS(x) inserter(x,x.begin())
12
13 using namespace std;
14
15 typedef set<int> Set;
16 map<Set,int> IDcache;
17 vector<Set> Setcache;
18
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
26 int main(void)
27 {
28     stack<int> s;
29     int n;
30     int T;
31     cin>>T;
32     while(T--)
33     {
34         cin>>n;
35         while(n--)
36         {
37             char op[10];
38             scanf("%s",op);
39             if(op[0]==‘P‘)
40                 s.push(ID(Set()));
41             else if(op[0]==‘D‘)
42                 s.push(s.top());
43             else
44             {
45                 Set x1=Setcache[s.top()];s.pop();
46                 Set x2=Setcache[s.top()];s.pop();
47                 Set x;
48                 if(op[0]==‘U‘)
49                     //x=set_union(ALL(x1),ALL(x2),INS(x));
50                     set_union(ALL(x1),ALL(x2),INS(x));
51                 if(op[0]==‘I‘)
52                     //x=set_intersection(ALL(x1),ALL(x2),INS(x));
53                     set_intersection(ALL(x1),ALL(x2),INS(x));
54                 if(op[0]==‘A‘)
55                 {x=x2;x.insert(ID(x1));}
56                 s.push(ID(x));
57             }
58             cout<<Setcache[s.top()].size()<<endl;
59         }
60         cout<<"***"<<endl;
61     }
62     return 0;
63 }

UVA 12096 STL map set 的使用

时间: 2025-01-02 22:55:13

UVA 12096 STL map set 的使用的相关文章

UVa 12096 (STL) The SetStack Computer

题意: 有一个集合栈计算机,栈中的元素全部是集合,还有一些相关的操作.输出每次操作后栈顶集合元素的个数. 分析: 这个题感觉有点抽象,集合还能套集合,倒是和题中配的套娃那个图很贴切. 把集合映射成ID,就可以用 stack<int>来模拟题中的集合栈,然后用 vector<Set> 来根据下标进行集合的索引. 代码虽短,但还须多体会. 1 #include <cstdio> 2 #include <string> 3 #include <vector&

UVa 156 (stl map的使用)

  一.map map就是从键(key)到值(value)的映射,重载了[]所以可以认为是高级版的数组,常用的一些操作如下: 头文件:#include<map> 定义:map <类型一(key),类型二(value)> name  key称为map的frist,value称为map的second. 初始化:name.clear(); 二.题目 Ananagrams Most crossword puzzle fans are used to anagrams--groups 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

stl::map之const函数访问

如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const { if (_map_name_value.find(name) != _map_name_value.end()) { return _map_name_value[name]; } return ""; } 上面的代码会报错:error C2678: 二进制“[”: 没有找到接受“c

hdu 4941 Magical Forest(STL map &amp; 结构体运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 220    Accepted Submission(s): 105 Problem Description There is a forest c

(转载)STL map与Boost unordered_map的比较

原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合适的位置插入到树中.所以,如果对map进行遍历(中序遍历)的话,输出的结果是有序的.顺序就是按照operator< 定义的大小排序.而boost::unordered_map是计算元素的Hash值,根据Hash值判断元素是否相同.所以,对unordered_map进行遍历,结果是无序的. 用法的区别就是

ZOJ1109_Language of FatMouse(STL/map)

解题报告 题意: 略. 思路: map应用. #include <algorithm> #include <iostream> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <map> using namespace std; map<string,stri

POJ训练计划3096_Surprising Strings(STL/map)

解题报告 题目传送门 题意: 给一个字符串,要求,对于这个字符串空隔为k取字符对(k=0,1,2,3,4...)要求在相同的空隔取对过程汇总,整个字符串中没有一个相同字符对如: ZGBZ: 间隔为0的字符对有: ZG.GB.BZ,三个均不相同 间隔为1的字符对有: ZG. GZ,均不相同 间隔为2的字符对有: ZZ 仅有一个,不必比较. 这种字符串定义为"surprising". 之后按照格式输出. 思路: map暴力. #include <iostream> #inclu

支持泛型AVL Tree的简单实现,并和STL map比较了插入,删除,查找的性能

1.问题描述: 1)AVL tree是一种自平衡树.它通过左右子树的高度差来控制树的平衡,当高度差是不大于1的时候,认为树是平衡的.树的平衡保证了树在极端情况下 (输入序列不够随机)的性能.很显然当左右子树高度平衡,保证了任何插入,删除,查找操作平均性能呢个,当不平衡时(有的子树很高),当 要操作的元素在这个子树时,性能会很差: 2)AVL tree 和Red black tree 都是一种平衡树,它的操作的时间复杂度是:O(lgN) ,N是树的节点的数目: 3)本文实现了AVL Tree, 并