首页 > 其他 > 详细

UVA - 12504

时间:2014-11-19 07:02:42      阅读:184      评论:0      收藏:0      [点我收藏+]
  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 ( Tbubuko.com,布布扣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}

 

Sample Output 

 

+d,ee
-b,f
*c

No changes

-first

 

 

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <map>
 5 
 6 using namespace std;
 7 
 8 map <string,string> ValueCatchOne;
 9 map <string,string> ValueCatchTwo;
10 
11 void Work(map<string,string>& IDcatch,string str) {
12     int ok = 0;
13     string key = "",value = "";
14     for (int i = 1,len = str.length();i < len;i++) {
15         if (ok == 0) {
16             if (str[i] == :) {
17                 ok = 1;
18                 continue;
19             }
20             key += str[i];
21         }
22         else {
23             if (str[i] != , && str[i] != }) value += str[i];
24             else {
25                 ok = 0;
26                 IDcatch[key] = value;
27                 key = "";value = "";
28             }
29         }
30     }
31 }
32 
33 int main () {
34     int T;
35     // freopen("1.in","r",stdin);
36     cin >> T;
37     getchar();
38     while (T--) {
39         string str;
40         ValueCatchOne.clear();
41         ValueCatchTwo.clear();
42         cin >> str;
43         Work(ValueCatchOne,str);
44         cin >> str;
45         Work(ValueCatchTwo,str);
46         if (ValueCatchOne.size() == ValueCatchTwo.size()) {
47             int ok = 1;
48             for (map<string,string>::iterator Iter = ValueCatchOne.begin();Iter != ValueCatchOne.end();Iter++) {
49                 if (ValueCatchTwo.count(Iter -> first) == 0 || ValueCatchTwo[Iter->first] != Iter -> second) {
50                     ok = 0;
51                     break;
52                 }
53             }
54             if (ok == 1) {
55                 cout << "No changes" << endl;
56                 cout << endl;
57                 continue;
58             }
59         }
60         {
61             int num = 0;
62             for (map<string,string>::iterator Iter = ValueCatchTwo.begin();Iter != ValueCatchTwo.end();Iter++) {
63                 if (ValueCatchOne.count(Iter -> first) == 0) {
64                     cout << (!num?+:,) << Iter -> first;
65                     num++;
66                 }
67             }
68             if (num) cout << endl;
69             num = 0;
70             for (map<string,string>::iterator Iter = ValueCatchOne.begin();Iter != ValueCatchOne.end();Iter++) {
71                 if (ValueCatchTwo.count(Iter -> first) == 0) {
72                     cout << (!num?-:,) << Iter -> first;
73                     num++;
74                 }
75             }
76             if (num) cout << endl;
77             num = 0;
78             for (map<string,string>::iterator Iter = ValueCatchOne.begin();Iter != ValueCatchOne.end();Iter++) {
79                 if (ValueCatchTwo.count(Iter -> first) && ValueCatchTwo[Iter -> first] != Iter -> second) {
80                     cout << (!num?*:,) << Iter -> first;
81                     num++;
82                 }
83             }
84             if (num) cout << endl;
85         }
86         cout << endl;
87     }    
88 }
View Code

 

UVA - 12504

原文:http://www.cnblogs.com/xiaoshanshan/p/4106993.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!