Updating a Dictionary

Problem C. Updating a Dictionary

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:
{key:value,key:value,...,key:value}
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 (T<=1000). 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}

 Output for Sample Input
+d,ee
-b,f
*c
No changes

 1 #include <iostream>
 2 #include <sstream>
 3 #include <map>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 void make_dict(string s, map<string,string>& m, vector<string>& keys) {
 9     for(int i = 0; i < s.size(); i++) {
10         if(!isalpha(s[i]) && !isdigit(s[i])) s[i] = ' ';
11     }
12     stringstream ss(s);
13     string key, value;
14     while(ss >> key) {
15         ss >> value;
16         m[key] = value;
17         keys.push_back(key);
18     }
19 }
20 
21 void print_list(const vector<string> v) {
22     for(int i = 0; i < v.size(); i++) {
23         if(i != 0) cout << ',';
24         cout << v[i];
25     }
26     cout << '\n';
27 }
28 
29 int main() {
30     int T;
31     cin >> T;
32     while(T--) {
33         string d1, d2;
34         map<string,string> m1, m2;
35         vector<string> all, added, removed, changed;
36         cin >> d1 >> d2;
37         make_dict(d1, m1, all);
38         make_dict(d2, m2, all);
39         sort(all.begin(), all.end());
40         all.erase(unique(all.begin(), all.end()), all.end());
41         for(int i = 0; i < all.size(); i++) {
42             string key = all[i];
43             if(!m1.count(key) && m2.count(key)) added.push_back(key);
44             if(m1.count(key) && !m2.count(key)) removed.push_back(key);
45             if(m1.count(key) && m2.count(key) && m1[key] != m2[key]) changed.push_back(key);
46         }
47         int flag = 0;
48         if(!added.empty()) {
49             flag = 1;
50             cout << "+";
51             print_list(added);
52         }
53         if(!removed.empty()) {
54             flag = 1;
55             cout << "-";
56             print_list(removed);
57         }
58         if(!changed.empty()) {
59             flag = 1;
60             cout << "*";
61             print_list(changed);
62         }
63         if(!flag) cout << "No changes\n";
64         cout << "\n";
65     }
66     return 0;
67 }
View Code

 

 

posted @ 2013-05-01 23:29  1002liu  阅读(242)  评论(0编辑  收藏  举报