算法竞赛入门经典 第二版 习题5-11 更新字典 Updating a Dictionary uva12504
来源:互联网 发布:中国经济数据网 编辑:程序博客网 时间:2024/04/30 15:41
题目:https://vjudge.net/problem/UVA-12504
思路:用集合的思想,旧字典与新字典的差集就是减少部分,新字典与旧字典的差集就是增加部分,新旧字典的交集中键值改变的部分就是修改部分。
注:读入时别忘单独处理空集。
代码:C++11
#include <iostream>#include <string>#include <cstring>#include <cctype>#include <cstdio>#include <set>#include <map>#include <algorithm>using namespace std;map<string, string> oridata;map<string, string> newdata;set<string> orikey;set<string> newkey;set<string> increase;set<string> decrease;set<string> change;void getdata(map<string, string> &nowdata, set<string> &nowkey){ bool keyflag = true; getchar(); string key, value; while(1) { char c; c = getchar(); if(c=='}') { if(keyflag) { getchar(); return ; } nowdata[key] = value; nowkey.insert(key); getchar(); return ; } else if(c==':') { keyflag = !keyflag; } else if(c==',') { keyflag = !keyflag; nowdata[key] = value; nowkey.insert(key); key.clear(); value.clear(); } else if(keyflag) { key.push_back(c); } else { value.push_back(c); } }}void print(set<string> key){ bool first = true; for(auto &t:key) { if(first) { first = false; } else { printf(","); } cout << t << flush; } cout << endl;}int main(){ int T; cin >> T; getchar(); while(T--) { oridata.clear(); newdata.clear(); orikey.clear(); newkey.clear(); increase.clear(); decrease.clear(); change.clear(); getdata(oridata, orikey); getdata(newdata, newkey); set_difference(newkey.begin(), newkey.end(), orikey.begin(), orikey.end(), inserter(increase, increase.begin())); set_difference(orikey.begin(), orikey.end(), newkey.begin(), newkey.end(), inserter(decrease, decrease.begin())); set<string> intersection; set_intersection(orikey.begin(), orikey.end(), newkey.begin(), newkey.end(), inserter(intersection, intersection.begin())); for(auto &t: intersection) { if(oridata[t]!=newdata[t]) { change.insert(t); } } if(increase.empty()&&decrease.empty()&&change.empty()) { cout << "No changes" << endl; } if(!increase.empty()) { printf("+"); print(increase); } if(!decrease.empty()) { printf("-"); print(decrease); } if(!change.empty()) { printf("*"); print(change); } cout << endl; } return 0;}
0 0
- 算法竞赛入门经典 第二版 习题5-11 更新字典 Updating a Dictionary uva12504
- UVa12504 习题5-11 更新字典 (Updating a Dictionary)
- [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary
- uva12504 Updating a Dictionary
- UVA12504 - Updating a Dictionary
- UVA12504 Updating a dictionary
- UVA12504--Updating a Dictionary
- 5-11 UVA 12504 Updating a dictionary更新字典
- 更新字典(Updating a Dictionary)
- 【清单】 ---算法竞赛入门经典第二版 【例题+习题】【持续更新中..】
- 算法竞赛入门经典第二章习题
- 算法竞赛入门经典习题 第二章
- 算法竞赛入门经典(第二版) 习题训练
- 【算法竞赛入门经典第二版学习】第三章习题
- 算法竞赛入门经典第二版第三章习题
- 算法竞赛入门经典(第二版) 习题
- 算法竞赛入门经典(第二版)第一章课后习题
- UVa 12504 Updating a Dictionary(更新字典)
- rails 路由(3)
- 一些杂乱的知识点(未整理)
- a class's lifetime
- redhat安装上安装和配置jdk1.8详细步骤
- UVALive 3683 A Scheduling Problem(树形DP)
- 算法竞赛入门经典 第二版 习题5-11 更新字典 Updating a Dictionary uva12504
- java中的值传递和引用传递
- Linux中关于init.d分析
- poj 2104 K-th Number (分桶法和平方分割)
- 什么是机器学习:一次权威定义之旅
- Java NIO笔记之Buffer
- 多线程2--毕向东基础视频教程学习笔记
- dpdk内存管理之内存初始化(内存收集)
- kernel32.dll函数介绍