[刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary

时间:2024-01-19 08:03:50

题意:对比新老字典的区别:内容多了、少了还是修改了。


代码:(Accepted,0.000s)

//UVa12504 - Updating a Dictionary
//#define _XieNaoban_
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<map>
using namespace std; int T;
int main()
{
#ifdef _XieNaoban_
freopen("in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin >> T;
cin.ignore();
while (T--) {
map<string, string> dic;
for (int i(0);i < 2;++i) {
string line, key, value;
getline(cin, line);
for (auto &r : line)
if (r == ',' || r == ':' || r == '{' || r == '}') r = ' ';
istringstream in(line);
while (in >> key) {
in >> value;
if (i) {
auto d(dic.find(key));
if (d != dic.end()) {
if (d->second != value) d->second = "*";//changed
else d->second = '&';//not changed
}
else dic[key] = "+";//increased
}
else dic[key] = value;
}
}
vector<string> inc, rmv, chg;
for (const auto& r : dic)
switch (r.second[0])
{
case '+': inc.push_back(r.first);break;
case '*': chg.push_back(r.first);break;
case '&': break;
default: rmv.push_back(r.first);break;
}
if (inc.empty() && rmv.empty() && chg.empty())
cout << "No changes\n";
else {
if (!inc.empty()) {
cout << '+' << inc[0];
for (auto i(inc.begin() + 1);i != inc.end();++i)
cout << ',' << *i;
cout << '\n';
}
if (!rmv.empty()) {
cout << '-' << rmv[0];
for (auto i(rmv.begin() + 1);i != rmv.end();++i)
cout << ',' << *i;
cout << '\n';
}
if (!chg.empty()) {
cout << '*' << chg[0];
for (auto i(chg.begin() + 1);i != chg.end();++i)
cout << ',' << *i;
cout << '\n';
}
}
cout << '\n';
}
return 0;
}

分析:这题很水,轻松AC,没啥好说的。