POJ 3297 Open Source STL(map+set+pair)
来源:互联网 发布:wifi连网神器mac版 编辑:程序博客网 时间:2024/06/02 01:35
为了熟悉STL,这道题用到的东东还挺多的,存档~
感谢小灰灰的好东西,囊括了许多STL的基本用法。
原题见POJ 3297
大写的是项目名,小写的是参与此项目的人名,两者一对多。所以想到了用map<string, set<> >
来存。
如果有在多个项目下留名的,则此人无效;若一人在一项目下重复留名,只记一次。人和编号对应起来,用map<string, int>
存,另开一个数组,给一个人留的项目计数,之后备用。同时确定了上面那个map里set的类型map<string, set<int> >
。
将项目以及人数存在pair<int, second>
下,答案放在vector<pair<int, string> >
下,按照first递减,second递增的方式输出。需要构造比较函数cmp
,在sort里引用。
sort默认按照first、second为第一、二关键字排序输出。如果加个greater<type>()
则逆序输出。本题要重写。
PS:不能用auto!打类型好烦…
附代码
/*-------------------------------------------- * File Name: POJ 3297 * Author: Danliwoo * Mail: Danliwoo@outlook.com * Created Time: 2016-05-25 00:48:25--------------------------------------------*/#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <algorithm>#include <cmath>#include <map>#include <set>#define N 10010using namespace std;map <string, int> stu;map <string, set<int> > pro;vector< pair <int, string> >ans;int a[N], n, m;string str, book;void init(){ stu.clear(); book.clear(); pro.clear(); ans.clear(); n = m = 0; memset(a, 0, sizeof(a));}bool cmp(const pair<int, string> &lhs, const pair<int, string> &rhs){ if(rhs.first == lhs.first) return lhs.second < rhs.second; return lhs.first > rhs.first;}void solve(){ std::map<string, set<int> >::iterator it; std::set<int>::iterator q; for(it = pro.begin(); it != pro.end();it++){ int cnt = 0; for(q = it->second.begin(); q != it->second.end();q++) if(a[*q] == 1) cnt++; ans.push_back(make_pair(cnt, it->first)); } sort(ans.begin(), ans.end(), cmp); // sort(ans.begin(), ans.end(), greater<pair <int, string> >()); vector< pair <int, string> >::iterator ak; for(ak = ans.begin();ak != ans.end();ak++) cout << ak->second << " " << ak->first << endl;}int main(){ init(); while(getline(cin, str)){ if(str == "0") break; if(str == "1") { solve(); init(); continue; } if(str[0] >= 'A' && str[0] <= 'Z'){ book = str; pro[book].insert(-1); } else{ map <string, int >::iterator iter = stu.find(str); if(iter == stu.end()) stu[str] = m++; std::set<int>::iterator q = pro[book].find(stu[str]); if(q == pro[book].end()){ a[stu[str]]++; pro[book].insert(stu[str]); } } } return 0;}
0 0
- POJ 3297 Open Source STL(map+set+pair)
- stl-map stl-pair
- poj 3297 Open Source
- STL之关联容器(pair、map、set的使用)
- STL之关联容器(pair、map、set的使用)
- stl-map-pair
- [poj 3481]Double Queue[set || map][STL]
- 【map】【set】poj 3297
- STL 之 pair 和map
- STL笔记:map and pair
- STL <set><map>
- stl map set
- STL map&set
- STL set map容器
- 【STL】 map、set;
- STL浅析set&map
- STL中的set&&map
- stl-map/set
- Atitit.png 图片不能显示 php环境下
- easyUI tabs切换
- Rest API 设计最佳实践(1)
- <机器学习练习>K-means聚类算法
- odoo8-qweb report 制作pdf报表方法介绍
- POJ 3297 Open Source STL(map+set+pair)
- atitit.身份认证解决方案attilax总结
- 关于多态的理解
- Lua 编程(一) 环境配置
- Android中线程那些事
- Android Drawable - Scale Drawable使用详解(附图)
- hdu2099
- Atitit.工作流 与 规则引擎
- 微信开发建站所需档案清单