湖南省第八届程序设计竞赛C

Problem C. Updating a Dictionary

 

In this problem, a dictionary is collectionof key-value pairs, where keys are lower-case letters, and values are non-negativeintegers. Given an old dictionary and a new dictionary, find out what werechanged.

 

Each dictionary is formatting asfollows:

 

{key:value,key:value,...,key:value}

 

Each key is a string oflower-case letters, and each value is a non-negative integer without leadingzeros or prefix '+'. (i.e. -4, 03 and +77 are illegal). Each key will appear atmost once, but keys can appear in any order.

 

Input

The first line contains thenumber of test cases T (T<=1000). Each test case contains twolines. The first line contains the old dictionary, and the second line containsthe new dictionary. Each line will contain at most 100 characters and will notcontain any whitespace characters. Both dictionaries could be empty.

 

WARNING: there are no restrictions on thelengths of each key and value in the dictionary. That means keys could bereally long and values could be really large.

 

Output

For eachtest case, print the changes, formatted as follows:

l  First, if there are any new keys,print '+' and then the new keys in increasing order (lexicographically),separated by commas.

l  Second, if there are any removedkeys, print '-' and then the removed keys in increasing order(lexicographically), separated by commas.

l  Last, if there are any keys withchanged value, print '*' and then these keys in increasing order(lexicographically), separated by commas.

 

If thetwo dictionaries are identical, print 'No changes' (without quotes) instead.

 

Print ablank line after each test case.

 

Sample Input                                       Output for 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}

+d,ee

-b,f

*c

 

No changes

 

-first

 

代码:

// Rujia Liu
#include<iostream>
#include<string>
#include<sstream>
#include<cctype>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

void make_dict(string s, map<string,string>& m, vector<string>& keys) {
  for(int i = 0; i < s.size(); i++) {
    if(!isalpha(s[i]) && !isdigit(s[i])) s[i] = ' ';
  }
  stringstream ss(s);
  string key, value;
  while(ss >> key) {
    ss >> value;
    m[key] = value;
    keys.push_back(key);
  }
}

void print_list(const vector<string> v) {
  for(int i = 0; i < v.size(); i++) {
    if(i != 0) cout << ',';
    cout << v[i];
  }
  cout << '\n';
}

int main() {
  int T;
  cin >> T;
  while(T--) {
    string d1, d2;
    map<string,string> m1, m2;
    vector<string> all, added, removed, changed;
    cin >> d1 >> d2;
    make_dict(d1, m1, all);
    make_dict(d2, m2, all);
    sort(all.begin(), all.end());
    all.erase(unique(all.begin(), all.end()), all.end());
    for(int i = 0; i < all.size(); i++) {
      string key = all[i];
      if(!m1.count(key) && m2.count(key)) added.push_back(key);
      if(m1.count(key) && !m2.count(key)) removed.push_back(key);
      if(m1.count(key) && m2.count(key) && m1[key] != m2[key]) changed.push_back(key);
    }
    int flag = 0;
    if(!added.empty()) { flag = 1; cout << "+"; print_list(added); }
    if(!removed.empty()) { flag = 1; cout << "-"; print_list(removed); }
    if(!changed.empty()) { flag = 1; cout << "*"; print_list(changed); }
    if(!flag) cout << "No changes\n";
    cout << "\n";
  }
  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值