《算法竞赛入门经典2ndEdition 》例题5-5 集合栈计算机(The SetStack Computer, Uva12096)
2015-12-29 21:20
417 查看
这道题一开始,根本就没有理解题意,看了半天Sample,一直傻傻地觉得Sample是错的,后来才意识到集合中相同的元素只能有一个,所以第五行是1,而不是2。于是理解了题意之后,瞬间思路被打开了,加上这是在STL这一章节,自然想到用STL。(不过还是看了眼程序才有了思路的)
热烈庆祝连续3道一次AC
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/09/3d38dd926faa7e9cd0ea134f4ee78b61)
下面是我的代码。
下面是刘汝佳标程。标程用的是Set到int的映射,我的程序用的是int到Set的映射。
热烈庆祝连续3道一次AC
下面是我的代码。
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <iostream> #include <set> #include <vector> #include <stack> #include <map> using namespace std; typedef set<int> Set; map<int, Set> ID; stack<int> st; int cnt; int getID(Set); int main() { freopen("New Text Document.txt","r",stdin); freopen("Output.txt","w",stdout); ios::sync_with_stdio(false); int t, n; string s; cin>>t; cin.get(); while(t--) { cnt = 0; while(!st.empty()) st.pop(); //ID.clear(); cin>>n; while(n--) { cin>>s; //cout<<s<<endl; if(s[0] == 'P') st.push(getID(Set())); else if(s[0] == 'D') st.push(st.top()); else { Set s1 = ID[st.top()]; st.pop(); Set s2 = ID[st.top()]; st.pop(); Set s3; s3.clear(); if(s[0] == 'U') { set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), insert_iterator<Set>(s3, s3.begin())); st.push(getID(s3)); } if(s[0] == 'I') { set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), insert_iterator<Set>(s3, s3.begin())); st.push(getID(s3)); } if(s[0] == 'A') { s2.insert(getID(s1)); st.push(getID(s2)); } } Set s = ID[st.top()]; cout<<s.size()<<endl; } //cout<<cnt; //while(!st.empty()) // { // cout<<ID[st.top()].size()<<endl; // st.pop(); // } //for(int i = 1; i <= cnt; i++) //cout<<ID[i].size(); cout<<"***"<<endl; } return 0; } int getID(Set s) { for(int i = 1; i <= cnt; i++) if(ID[i] == s) return i; ID[++cnt] = s;//之前一直没写这个导致出错,没有将新集合加入到映射数组ID中 return cnt; }
下面是刘汝佳标程。标程用的是Set到int的映射,我的程序用的是int到Set的映射。
// UVa12096 The SetStack Computer // Rujia Liu #include<iostream> #include<string> #include<set> #include<map> #include<stack> #include<vector> #include<algorithm> using namespace std; #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) typedef set<int> Set; map<Set,int> IDcache; // 把集合映射成ID vector<Set> Setcache; // 根据ID取集合 // 查找给定集合x的ID。如果找不到,分配一个新ID int ID (Set x) { if (IDcache.count(x)) return IDcache[x]; Setcache.push_back(x); // 添加新集合 return IDcache[x] = Setcache.size() - 1; } int main () { int T; cin >> T; while(T--) { stack<int> s; // 题目中的栈 int n; cin >> n; for(int i = 0; i < n; i++) { string op; cin >> op; if (op[0] == 'P') s.push(ID(Set())); else if (op[0] == 'D') s.push(s.top()); else { Set x1 = Setcache[s.top()]; s.pop(); Set x2 = Setcache[s.top()]; s.pop(); Set x; if (op[0] == 'U') set_union (ALL(x1), ALL(x2), INS(x)); if (op[0] == 'I') set_intersection (ALL(x1), ALL(x2), INS(x)); if (op[0] == 'A') { x = x2; x.insert(ID(x1)); } s.push(ID(x)); } cout << Setcache[s.top()].size() << endl; } cout << "***" << endl; } return 0; }
相关文章推荐
- C++基于栈实现铁轨问题
- C语言栈的表示与实现实例详解
- C语言实现颠倒栈的方法
- 算法系列15天速成 第十天 栈
- 一看就懂:图解C#中的值类型、引用类型、栈、堆、ref、out
- Array栈方法和队列方法的特点说明
- java数据结构之java实现栈
- 浅析栈区和堆区内存分配的区别
- 用Java代码实现栈数据结构的基本方法归纳
- 用PHP解决的一个栈的面试题
- 浅谈C#中堆和栈的区别(附上图解)
- JavaScript数据结构与算法之栈详解
- C语言之栈和堆(Stack && Heap)的优缺点及其使用区别
- Python实现栈的方法
- Java中堆和栈的区别详解
- java中堆和栈的区别分析
- C/C++函数调用栈的实现方法
- C++中静态存储区与栈以及堆的区别详解
- C++中用栈来判断括号字符串匹配问题的实现方法
- C++中栈结构建立与操作详细解析