题目: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;
}