In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, and values are non-negative integers. Given an old dictionary and a new dictionary, find out what were changed.
Each dictionary is formatting as follows:
Each key is a string of lower-case letters, and each value is a non-negative integer without leading zeros or prefix `+'. (i.e. -4, 03 and +77 are illegal). Each key will appear at most once, but keys can appear in any order.
Input
The first line contains the number of test cases T ( T1000 ). Each test case contains two lines. The first line contains the old dictionary, and the second line contains the new dictionary. Each line will contain at most 100 characters and will not contain any whitespace characters. Both dictionaries could be empty.
WARNING: there are no restrictions on the lengths of each key and value in the dictionary. That means keys could be really long and values could be really large.
Output
For each test case, print the changes, formatted as follows:
- First, if there are any new keys, print `+' and then the new keys in increasing order (lexicographically), separated by commas.
- Second, if there are any removed keys, print `-' and then the removed keys in increasing order (lexicographically), separated by commas.
- Last, if there are any keys with changed value, print `*' and then these keys in increasing order (lexicographically), separated by commas.
If the two dictionaries are identical, print `No changes' (without quotes) instead.
Print a blank line after each test case.
Sample Input
3 {a:3,b:4,c:10,f:6} {a:3,c:5,d:10,ee:4} {x:1,xyz:123456789123456789123456789} {xyz:123456789123456789123456789,x:1} {first:1,second:2,third:3} {third:3,second:2}
Sample Output
+d,ee -b,f *c No changes -first
思路:
先取新旧字典中的每对键值对,再对其判断。
对于新增键,对于新字典中的每一个key,要从旧字典中查找其是否存在。
对于删除键,对于旧字典中的每一个key,要从新字典中查找其是否存在。
对于修改键,对于新旧字典的任意两对键值,判断它们是否键相同而值不同。
另外一种情况就是没有任何修改。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
string T[105];//存储满足某种要求的key
map<string,string>::iterator it;
void getmap(string s,map<string,string> &v)
{
string a,b;
for(int i = 0; i < 2; ++i) {
int j = 1, l = s.size();
while(l > 2 && j < l){
while(s[j] != ':') a += s[j++]; ++j;
while(s[j] != ',' && s[j] != '}') b += s[j++]; ++j;
v[a] = b;
a = b = "";
}
}
}
void print(int n,char c)
{
sort(T,T+n);//排序
cout<<c<<T[0];
for(int i=1;i<n;i++) cout<<","<<T[i];
puts("");
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++){
map<string,string> orig,curr;
map<string,string>::iterator it;
string line;
cin>>line; getmap(line,orig);
cin>>line; getmap(line,curr);
// for(it=curr.begin();it!=curr.end();it++)
// cout<<it->first<<" "<<it->second<<endl;
int x=0,y=0,z=0;//记录key的数目
for(it=curr.begin();it!=curr.end();it++)//新增键
if(!orig.count(it->first)) T[x++]=it->first;
if(x) print(x,'+');
for(it=orig.begin();it!=orig.end();it++)//删除键
if(!curr.count(it->first)) T[y++]=it->first;
if(y) print(y,'-');
for(it=orig.begin();it!=orig.end();it++)//修改键
if(curr.count(it->first)&&curr[it->first]!=it->second) T[z++]=it->first;
if(z) print(z,'*');
if(!(x||y||z))
puts("No changes");
puts("");
}
return 0;
}