Update a Dictionary map

来源:互联网 发布:济宁淘宝代运 编辑:程序博客网 时间:2024/06/07 18:59

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

题意:
给你一本旧字典和新字典,问添加、减少、改变了多少词
思路:使用两个map即可

原来处理输入的时候有bug,wa了好多发= =!!!

#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <map>#include <vector>#define MM(s,q) memset(s,q,sizeof(s))#define INF 0x3f3f3f3f#define MAXN 1005#define Lchild id<<1#define Rchild (id<<1)+1#define FILE  freopen("data.in","r",stdin)using namespace std;map<string, string>Old, New;string Add[MAXN], Sub[MAXN], Change[MAXN];int add, sub, change;void input(map<string, string> &A) {    A.clear();    string s, key, value;    int i = 1;    cin >> s;    int sign = 1;    for (int i = 0; i < s.size(); i++) {        if (s[i] == '{') continue;        if (s[i] == ':') {            sign = 0;            continue;        }        if (s[i] == ',' || s[i] == '}') {            if (key != "")A[key] = value;            key = value = "";            sign = 1;            continue;        }        if (sign) key += s[i];        else value += s[i];    }    /*    while (s[i] != '}') {        while (islower(s[i]))            key += s[i++];        i++;        while (isdigit(s[i]))            value += s[i++];        if (key != "")            A[key] = value;        key = value = "";    }*/}void Find() {    add = sub = change = 0;    string a, b;    map<string, string>::iterator x = Old.begin(), y = New.begin();    while (x != Old.end() && y != New.end()) {        a = x->first, b = y->first;        if (a == b) {            if (x->second != y->second)                Change[change++] = a;            x++, y++;        } else if (a < b) {            Sub[sub++] = a;            x++;        } else if (a > b) {            Add[add++] = b;            y++;        }    }    while (x != Old.end()) Sub[sub++] = x->first, x++;    while (y != New.end()) Add[add++] = y->first, y++;}int main() {    // FILE;    int T;    cin >> T;    while (T--) {        input(Old);        input(New);        Find();        if (New == Old) {            printf("No changes\n\n");            continue;        }        /*        add = sub = change = 0;        for (map<string, string>::iterator it = New.begin(); it != New.end(); it++) {            if (!Old.count(it->first)) Add[add++] = it->first;            else if (Old[it->first] != it->second) Change[change++] = it->first;        }        for (map<string, string>::iterator it = Old.begin(); it != Old.end(); it++) {            if (!New.count(it->first)) Sub[sub++] = it->first;        }        */        if (add) {            cout << "+";            for (int i = 0; i < add; i++)                printf("%s%c", Add[i].c_str(), i == add - 1 ? '\n' : ',');        }        if (sub) {            cout << "-";            for (int i = 0; i < sub; i++)                printf("%s%c", Sub[i].c_str(), i == sub - 1 ? '\n' : ',');        }        if (change) {            cout << "*";            for (int i = 0; i < change; i++)                printf("%s%c", Change[i].c_str(), i == change - 1 ? '\n' : ',');        }        printf("\n");    }}
0 0
原创粉丝点击