#include <bits/stdc++.h> using namespace std; struct word { string str; int cnt; word(string _s,int _c):str(_s),cnt(_c){} }; void splitString(const string& s, vector<word>& v, const string& c) { string::size_type pos1, pos2, pos; pos2 = s.find(c); pos1 = 0; string tmp; while (string::npos != pos2) { //v.push_back(s.substr(pos1, pos2 - pos1)); tmp = s.substr(pos1, pos2 - pos1); pos = tmp.find(':'); v.push_back(word(tmp.substr(0, pos), stoi(tmp.substr(pos + 1)))); pos1 = pos2 + c.size(); pos2 = s.find(c, pos1); } if (pos1 != s.size()) { tmp = s.substr(pos1, pos2 - pos1); pos = tmp.find(':'); v.push_back(word(tmp.substr(0, pos), stoi(tmp.substr(pos + 1)))); } } int main() { string str; while (cin >>str) { auto pos = str.find('@'); string str1, str2; str1 = str.substr(0, pos); str2 = str.substr(pos + 1); //cout << str1 << " " << str2 << endl; vector<word> all_vec; splitString(str1, all_vec, ","); vector<word> used_vec; splitString(str2, used_vec, ","); map<string, int> m; for (auto it : used_vec) { m[it.str] = it.cnt; } string res; for (int i = 0; i < all_vec.size(); ++i) { if (m.find(all_vec[i].str) != m.end()) all_vec[i].cnt -= m[all_vec[i].str]; if (all_vec[i].cnt>0) { res += all_vec[i].str + ":" + to_string(all_vec[i].cnt) + ","; } } if (!res.empty() && res[res.size() - 1] == ',') res = res.substr(0, res.size()-1); cout << res << endl; } return 0; } /* a:3,b:5,c:2@a:1,b:2 */
点赞 1

相关推荐

牛客网
牛客企业服务