CSU 1113 Updating a Dictionary

来源:互联网 发布:keynote软件下载 编辑:程序博客网 时间:2024/06/05 12:01

CSU 1113 Updating a Dictionary


Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 777 Solved: 187

Description

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}

Sample Output

+d,ee
-b,f
*c

No changes

-first

#include <iostream>#include <cstdio>#include <string>#include <map>#include <vector>using namespace std;const int MAX_NUM = 100 + 5;void print(const vector<string>& p, char c) {    if(!p.empty()) {        cout << c << p[0];        for(int i = 1; i < p.size(); i++) {            cout << "," << p[i];        }        cout << endl;    }}int main() {    int T;    cin >> T;    while(T--) {        string s;        cin >> s;        map<string, string> dic;        map<string, string> dic2;        if(s.length() > 2) {            s = s.substr(1, s.length() - 2);            int pos = 0;            string cs[MAX_NUM];            int num = 0;            while((pos = s.find(',')) != s.npos) {                cs[num++] = s.substr(0, pos);                s = s.substr(pos + 1, s.length() - pos);            }            cs[num++] = s;            for(int i = 0; i < num; i++) {                s = cs[i];                pos = s.find(':');                dic[s.substr(0, pos)] = s.substr(pos + 1, s.length() - pos);            }//            map<string, string>::iterator it;(迭代器)//            for(it = dic.begin(); it != dic.end(); it++) {//                cout << it->first << " " << it->second << endl;//            }        }        cin >> s;        if(s.length() > 2) {            s = s.substr(1, s.length() - 2);            int pos = 0;            string cs[MAX_NUM];            int num = 0;            while((pos = s.find(',')) != s.npos) {                cs[num++] = s.substr(0, pos);                s = s.substr(pos + 1, s.length() - pos);            }            cs[num++] = s;            for(int i = 0; i < num; i++) {                s = cs[i];                pos = s.find(':');                dic2[s.substr(0, pos)] = s.substr(pos + 1, s.length() - pos);            }//            map<string, string>::iterator it;(迭代器)//            for(it = dic2.begin(); it != dic2.end(); it++) {//                cout << it->first << " " << it->second << endl;//            }        }        vector<string> plu;        map<string, string>::iterator it;        for(it = dic2.begin(); it != dic2.end(); it++) {            if(!dic.count(it->first)) {                plu.push_back(it->first);            }        }        vector<string> subs;        for(it = dic.begin(); it != dic.end(); it++) {            if(!dic2.count(it->first)) {                subs.push_back(it->first);            }        }        vector<string> update;        for(it = dic2.begin(); it != dic2.end(); it++) {            if(dic.count(it->first)) {                if(dic[it->first] != dic2[it->first]) {                    update.push_back(it->first);                }            }        }        if(plu.empty() && subs.empty() && update.empty()) {            cout << "No changes" << endl;        } else {            print(plu, '+');            print(subs, '-');            print(update, '*');        }        if(T != 0) {            cout << endl;        }    }    return 0;}
0 0
原创粉丝点击