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

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}

HINT

思路很清晰,使用map<string,string>来存储键和值,一个用于存储新词典,一个用于储存旧词典。用两个数组分别储存修改的和新增的词。遍历新词典:

  • 如果新旧词典的键和值相同就删除旧词典的词
  • 如果新词典中有而旧词典中也有点值不相同,说明是修改的部分,将词存入数组,在旧词典中删除该词。
  • 如果旧词典中没有该词,说明是新增加的词,存入另一个数组,在旧词典中删除该词。
  • 遍历完成之后,旧词典中剩下的就是新词典中删除的词。

最后输出结果就可以了。

注意:每组数据之后都要输出一个空行,包括最后一行。

Accepted

#include<bits/stdc++.h>
using namespace std;
int main() {
ofstream fcout;
fcout.open("temp.txt");
int num;
string s, s1, s2;
map<string, string>old, news;
vector<string>newkey,rekey;
cin >> num;getchar();
while (num--){
int flag = 0;
old.clear();news.clear();rekey.clear();newkey.clear();
cin >> s1 >> s2;
for (int i = 0;i < s1.length();i++)if (s1[i] == '{' || s1[i] == '}' || s1[i] == ',' || s1[i] == ':')s1[i] = ' ';
for (int i = 0;i < s2.length();i++)if (s2[i] == '{' || s2[i] == '}' || s2[i] == ',' || s2[i] == ':')s2[i] = ' ';
stringstream ss1(s1), ss2(s2);
while (ss1 >> s1 >> s2)old[s1] = s2;
while (ss2 >> s1 >> s2)news[s1] = s2;
for (auto i = news.begin();i != news.end();i++ ) {
if (old.count(i->first) && old[i->first] == i->second)old.erase(i->first);
else if (old.count(i->first) && old[i->first] != i->second) { rekey.push_back(i->first);old.erase(i->first); }
else if (!old.count(i->first))newkey.push_back(i->first);
}
if (newkey.size()) {
for (int i = 0;i < newkey.size();i++) cout << (i == 0 ? '+' : ',') << newkey[i];
cout << endl;
}
if (old.size()) {
for (auto i = old.begin();i != old.end();i++)cout << (i == old.begin() ? '-' : ',') << i->first;
cout << endl;
}
if (rekey.size()) {
for (int i = 0;i < rekey.size();i++) cout << (i == 0 ? '*' : ',') << rekey[i];
cout << endl;
} if (!newkey.size() && !rekey.size() && !old.size())cout << "No changes" << endl;
cout << endl;
} }

Updating a Dictionary UVA - 12504的更多相关文章

  1. [ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]

      Updating a Dictionary  In this problem, a dictionary is collection of key-value pairs, where keys ...

  2. csuoj 1113: Updating a Dictionary

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 1113: Updating a Dictionary Time Limit: 1 Sec  ...

  3. 湖南生第八届大学生程序设计大赛原题 C-Updating a Dictionary(UVA12504 - Updating a Dictionary)

    UVA12504 - Updating a Dictionary 给出两个字符串,以相同的格式表示原字典和更新后的字典.要求找出新字典和旧字典的不同,以规定的格式输出. 算法操作: (1)处理旧字典, ...

  4. [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary

    题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...

  5. Problem C Updating a Dictionary

    Problem C     Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, ...

  6. Uva - 12504 - Updating a Dictionary

    全是字符串相关处理,截取长度等相关操作的练习 AC代码: #include <iostream> #include <cstdio> #include <cstdlib& ...

  7. 【UVA】12504 Updating a Dictionary(STL)

    题目 题目     分析 第一次用stringstream,真TMD的好用     代码 #include <bits/stdc++.h> using namespace std; int ...

  8. 【习题 5-11 UVA 12504 】Updating a Dictionary

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 不确定某个map里面是否有某个关键字的时候. 要用find来确定. 如果直接用访问下标的形式去做的话. 会强行给他加一个那个关键字( ...

  9. Uva 511 Updating a Dictionary

    大致题意:用{ key:value, key:value, key:value }的形式表示一个字典key表示建,在一个字典内没有重复,value则可能重复 题目输入两个字典,如{a:3,b:4,c: ...

随机推荐

  1. vue_webpack

    1.生成项目工程描述文件 npm init 2.安装webpack开发依赖 (本地安装):npm install -D 3.(webpack4.0版本以上安装webpack cli) npm inst ...

  2. Iterative learning control for linear discrete delay systems via discrete matrix delayed exponential function approach

    对于一类具有随机变迭代长度的问题,如功能性电刺激,用户可以提前结束实验过程,论文也是将离散矩阵延迟指数函数引入到状态方程中. 论文中关于迭代长度有三个定义值:\(Z^Ta\) 为最小的实验长度,\(Z ...

  3. 前端axios传递一个包含数组的对象到后台,后台可以用String接收,也可以用List集合接收

    前端代码: data() { return { listQuery: { date: [], } }}, //查询列表信息getList() { if (this.listQuery.date == ...

  4. oracle can't kill session

    oracle 在杀会话时,会出现杀不掉的情况. 原因是在回滚大事物   解决方法: alter system disconnect session 'sid, serial#' immediate; ...

  5. 如何吃透Python的面向对象(OOP)

    ​本篇可乐和大家一起来吃透 Python 的面向对象,类和实例. 面向对象(OOP) 解释:面向对象它是一种编程的思想,将现实事物抽象化为编程对象. 举例说明:喝可乐 ① 选择自己根据配方买对应的材料 ...

  6. CMD(命令提示符)的基本操作(文件)

    打开CMD窗口,接下来将介绍如何使用CMD来创建.删除.修改.查看文件 1.1 使用CMD创建空文件(为了更好的演示,本文皆以D盘为当前路径),命令如下: copy nul xxx.xx(文件名) 命 ...

  7. JAVA基础(零)—— 踩坑与错误(常更)

    JAVA基础(零)-- 踩坑与错误(常更) 1 坑 考虑输入为null的情况 自动转换 x/Math.pow(10,i)*Math.pow(10,i) //由于math.pow()返回double类型 ...

  8. 解决vue 绑定事件会覆盖默认参数的问题

    解决vue 绑定事件会覆盖默认参数的问题 在使用一些ui框架的时候,某些组件的框架中的事件所规定的参数不能满足实际开发的需要,但是直接传入参数会把默认的参数覆盖掉 解决方法:将参数放入箭头函数中,传递 ...

  9. 49元起!魅族Lipro LED灯泡发布:无可视频闪、无积热问题

    转: 49元起!魅族Lipro LED灯泡发布:无可视频闪.无积热问题 魅族智能家居品牌Lipro今日办会,公布一批新品,均面向健康照明领域打造. 魅族强调,Lipro健康照明产品主打博物馆级健康光. ...

  10. Mac忘记密码

    1.启动电脑的时候,按住 Command+R,直到苹果的图标出现,松开,等待进入... 2.直接点击菜单栏上有个功能里面有 "终端" 功能,点击打开. 3.在终端页面里输入---& ...