Uva 12504-Updating a Dictionary(STL, map)
2015-09-08 18:22
357 查看
题目大意见紫书P136- -.
大致思路:这道题其实就是一个暴力,看一个map里面的元素的值再另一个map里面是不是相等就行了,至于题目似乎要求字典序。。废话么map本来就是排好序的所以完全不应在意。只不过我把找‘+’ 和 ‘-'写到一个函数里面来减少代码量,当然这种强行减少代码量会多出许多小trick需要花时间解决。。真正比赛赶时间的时候无脑暴力怎么省时间怎么来就行了。具体细节见代码中的注释(其实也没多少Orz)。
大致思路:这道题其实就是一个暴力,看一个map里面的元素的值再另一个map里面是不是相等就行了,至于题目似乎要求字典序。。废话么map本来就是排好序的所以完全不应在意。只不过我把找‘+’ 和 ‘-'写到一个函数里面来减少代码量,当然这种强行减少代码量会多出许多小trick需要花时间解决。。真正比赛赶时间的时候无脑暴力怎么省时间怎么来就行了。具体细节见代码中的注释(其实也没多少Orz)。
#include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <string> #include <cctype> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <sstream> #include <iostream> #include <algorithm> using namespace std; #define ls id<<1,l,mid #define rs id<<1|1,mid+1,r #define OFF(x) memset(x,-1,sizeof x) #define CLR(x) memset(x,0,sizeof x) #define MEM(x) memset(x,0x3f,sizeof x) #define rep(i,a,b) for (int i=a;i<b;i++) #define Rep(i,a,b) for (int i=a;i>=b;i--) typedef long long ll ; typedef pair<int,int> pii ; const int maxn = 1e5+50 ; const int inf = 0x3f3f3f3f ; const int MOD = 1e9+7 ; int T;char ch[2]={'-','+'}; map<string,string> mp[2]; vector<string> change; void get(int id) { string S; getline(cin,S); for (int i=0;i<S.length();i++) if (!isalpha(S[i])&&!isdigit(S[i])) S[i] = ' '; stringstream ss(S); string tmp1,tmp2; while (ss >> tmp1 >> tmp2 ) mp[id][tmp1] = tmp2; } bool Find(int x) { bool flag=0; int y=1-x; map<string,string>::iterator it=mp[x].begin(); while (it!=mp[x].end()) { string Name=it->first,val=it->second; if (mp[y].find(Name)==mp[y].end()) { if (!flag) {cout << ch[x] << Name ;flag=1;} else cout << ',' << Name ; } else if (x==0 && mp[x][Name]!=mp[y][Name]) change.push_back(Name);// Orz 本打算change也直接边找边输出的结果发现顺序不对所以必须存起来 it++; } if (flag) cout << '\n' ; return flag; } int main () { #ifdef LOCAL freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin); #endif cin >> T ; getchar(); while (T--) { mp[0].clear();mp[1].clear(); change.clear(); get(0);get(1); bool f1=Find(1);bool f2=Find(0);// 这里没记错的话是不能直接用 if (Find(0) || Find(1)) 的,不然的话Find(0)成立后就不会执行Find(1)了 if (!f2&&!f1&&!change.size()) cout << "No changes\n\n" ; else { for (int i=0;i<change.size();i++) if (!i) cout << '*' << change[i] ;else cout << ',' << change[i]; if (change.size()) cout << '\n' ; cout << '\n' ; } } return 0; }
相关文章推荐
- leetcode Remove Nth Node From End of List java编程学习(七)
- JavaScript中函数参数的值传递和引用传递
- 得知OpenCV研究报告指出系列(一)VS2010+OpenCV2.4.9环境配置
- UART的FIFO功能
- map 的遍历
- cocos2d js 使用 cocos studio 合图出现的黑线
- 导航栏修改透明度
- 一个对象null,gc后依然可以绝境求生
- Hadoop 工作原理
- Django 之 下载文件
- 面向对象学习--嵌套类(Nested Class)
- java 中关于System property 之 file.encoding
- 几句话弄清楚Java参数传值还是传引用
- JavaScript权威设计--JavaScript语言核心(简要学习笔记一)
- 算法—判断整数序列是不是二元查找树的后序遍历结果
- 邮箱账号@自动联想以及密码显隐
- sql 查询连续七天以上都有打卡的员工记录
- 摆弄【Nhibernate 协会制图--导乐陪伴分娩】
- JAVA SE 基础复习-基本程序设计(2)
- Python学习一入门