12504:Updating a Dictionary
version 1:
#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<cstring>
using namespace std;
const int maxl = 100 + 5;
int t,n;
char c;
void readkey(string& s,char st){
char a[maxl];
int cnt = 0;
for(c = getchar();c != st && c != '}';){
a[cnt++] = c;
c = getchar();
}
s.resize(cnt);
for(int i = 0;i < cnt;i++) s[i] = a[i];
}
int main(){
scanf("%d",&t);
while(t--){
set<string>okey,nkey;
map<string,string>olds,news;
string key,val;
getchar();getchar();
c = 0;
while(c != '}'){
readkey(key,':');
if(c == ':') readkey(val,',');
if(key.size()){
olds[key] = val;
okey.insert(key);
}
}
getchar();getchar();
c = 0;
while(c != '}'){
readkey(key,':');
if(c == ':') readkey(val,',');
if(key.size()){
news[key] = val;
nkey.insert(key);
}
}
int mark = 0,mark2 = 0;
for(set<string>::iterator it = nkey.begin();it != nkey.end();it++){
if(!olds.count(*it)){
if(!mark2) cout<<'+';
else cout<<',';
cout<<*it;
mark = mark2 = 1;
}
}
if(mark2) putchar('\n');
mark2 = 0;
for(set<string>::iterator it = okey.begin();it != okey.end();it++){
if(!news.count(*it)){
if(!mark2) cout<<'-';
else cout<<',';
cout<<*it;
mark = mark2 = 1;
}
}
if(mark2) putchar('\n');
mark2 = 0;
for(set<string>::iterator it = okey.begin();it != okey.end();it++){
if(news.count(*it) && news[*it] != olds[*it]){
if(!mark2) cout<<'*';
else cout<<',';
cout<<*it;
mark = mark2 = 1;
}
}
if(mark2) putchar('\n');
if(!mark) cout<<"No changes\n";
putchar('\n');
}
return 0;
}
Email:JingwangLi@outlook.com