一般的模拟题,一开始WA,可能是用string的容器放char,改成string就过了
14073581 | 12504 | Updating a Dictionary | Accepted | C++ | 0.032 | 2014-08-21 07:12:19 |
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<list> #include<cmath> #include<string> #include<sstream> #include<ctime> using namespace std; #define _PI acos(-1.0) #define INF (1 << 10) #define esp 1e-9 typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> pill; /*=========================================== ===========================================*/ #define MAXD 100 + 10 map<string,string>pre; map<string,string>now; vector<string>add; vector<string>cost; vector<string>change; vector<string>_str1; vector<string>_str2; void init(){ pre.clear(); now.clear(); add.clear(); cost.clear(); change.clear(); _str1.clear(); _str2.clear(); } void solve1(string str){ int L = str.size(); string key = "",value = ""; int ok = 0; for(int i = 1 ; i < L - 1; i++){ if(str[i] == ':'){ _str1.push_back(key); ok = 1; } else if(str[i] == ','){ pre[key] = value; value = ""; key = ""; ok = 0; } else if(!ok) key += str[i]; else value += str[i]; } pre[key] = value; } void solve2(string str){ int L = str.size(); string key = "",value=""; int ok = 0; for(int i = 1 ; i < L - 1; i++){ if(str[i] == ':'){ _str2.push_back(key); ok = 1; } else if(str[i] == ','){ now[key] = value; value = ""; key = ""; ok = 0; } else if(!ok) key += str[i]; else value += str[i]; } now[key] = value; } void solve(){ for(int i = 0 ; i < _str2.size() ; i++){ string str = _str2[i]; /*在新字典里面找*/ //cout << str <<" " << now[str] << endl; if(!pre.count(str)) /*旧字典没有这个*/ add.push_back(str); else if(pre[str] != now[str]) change.push_back(str); } for(int i = 0 ; i < _str1.size() ; i++){ string str = _str1[i]; /*在旧字典里面找*/ //cout << str <<" " << pre[str] << endl; if(!now.count(str)) /*新字典没有这个*/ cost.push_back(str); } } int main(){ int T; cin >> T; while(T--){ init(); string str; cin >> str; solve1(str); cin >> str; solve2(str); solve(); if(!add.size() && !cost.size() && !change.size()) cout << "No changes" << endl; else{ if(add.size()){ printf("+"); sort(add.begin(),add.end()); for(int i = 0 ; i < add.size() ; i++){ cout << add[i]; if(i < add.size() - 1) cout << ","; else cout << endl; } } if(cost.size()){ printf("-"); sort(cost.begin(),cost.end()); for(int i = 0 ; i < cost.size() ; i++){ cout << cost[i]; if(i < cost.size() - 1) cout << ","; else cout << endl; } } if(change.size()){ printf("*"); sort(change.begin(),change.end()); for(int i = 0 ; i < change.size() ; i++){ cout << change[i]; if(i < change.size() - 1) cout << ","; else cout << endl; } } } cout << endl; } return 0; }
【UVA】12504 - Updating a Dictionary(map,string,vector模拟)
时间: 2024-11-29 06:24:44