UVa - 12504 Updating a Dictionary
题意
更新字典
记录下第二个字典比第一个字典多的、少的、对应值不相等的
思路
map< string,string>存键值
用set< string> jia,jian,cheng;分别存下三种更新的键
注意特判空字典的情况 .
Both dictionaries could be empty.
AC代码
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
map<string,string> a,b;
set<string> jia,jian,cheng;
void insert_map( map<string,string> &a, string s )
{
int i = 1;
while( s[i] != '\0' )
{
int mrk = i;
while( s[i] != ':' )
i++;
string s1(s,mrk,i-mrk);
mrk = i+1;
while( s[i] != ',' && s[i] != '}' )
i++;
string s2(s,mrk,i-mrk);
i++;
a.insert(make_pair(s1,s2));
}
}
int main()
{
int T;
string s1, s2;
cin >> T;
while(T--){
s1 = "";
s2 = "";
cin >> s1;
cin >> s2;
if( s1 != "{}" ) insert_map(a,s1);
if( s2 != "{}" ) insert_map(b,s2);
map<string,string>::iterator it = a.begin();
for( ; it != a.end(); it++ ){
if( !b.count(it->first) )
jian.insert(it->first);
else
if( b[it->first] != a[it->first] )
cheng.insert(it->first);
}
it = b.begin();
for( ; it != b.end(); it++ )
if( !a.count(it->first) )
jia.insert(it->first);
set<string>::iterator iter;
if( jia.empty() && jian.empty() && cheng.empty() )
cout << "No changes" << endl;
if(!jia.empty()){
cout << '+' ;
iter = jia.begin();
for( ; iter != jia.end(); iter++ ){
if( iter != jia.begin() )
cout << ',';
cout << *iter ;
}
cout << endl;
jia.clear();
}
if(!jian.empty()){
cout << '-' ;
iter = jian.begin();
for( ; iter != jian.end(); iter++ ){
if( iter != jian.begin() )
cout << ',';
cout << *iter ;
}
cout << endl;
jian.clear();
}
if(!cheng.empty()){
cout << '*' ;
iter = cheng.begin();
for( ; iter != cheng.end(); iter++ ){
if( iter != cheng.begin() )
cout << ',';
cout << *iter ;
}
cout << endl;
cheng.clear();
}
cout << endl;
if(!a.empty()) a.clear();
if(!b.empty()) b.clear();
}
return 0;
}