Problem C     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 ( 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+set;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<map>
#include<set>
#define ll long long
using namespace std;
map<string,string>m;
set<string>p1;
set<string>p2;
string s,s1,s2;
int n,x;
int main()
{
cin>>n;
while(n--)
{
m.clear();
p1.clear();
p2.clear();
s1.clear();
s2.clear();
cin>>s;
for(int i=;s[i];i++)
{
if(isalpha(s[i]))
s1=s1+s[i];
else if(isdigit(s[i]))
{
s2=s2+s[i];
}
else if(s[i]==','||s[i]=='}')
{
if(s1!=""&&s2!="")
m[s1]=s2;
s1.clear();
s2.clear();
}
}
s.clear();
cin>>s;
for(int i=;s[i];i++)
{
if(isalpha(s[i]))
s1=s1+s[i];
else if(isdigit(s[i]))
{
s2=s2+s[i];
}
else if(s[i]==','||s[i]=='}')
{
if(m.count(s1)&&m[s1]==s2)//存在
{
m.erase(s1);
s1.clear();
s2.clear();
continue;
}
else if(m.count(s1)&&m[s1]!=s2)//更改
{
p2.insert(s1);
m.erase(s1);
}
else if(m.count(s1)==&&s1!="")//增加
p1.insert(s1); s1.clear();
s2.clear();
}
}
set<string>::iterator itt;
if(!p1.empty())//增加
{
cout<<'+';
for(itt=p1.begin();itt!=p1.end();itt++)
{
if(itt==p1.begin())
cout<<*itt;
else
cout<<','<<*itt;
}
cout<<endl;
}
if(!m.empty())
{
cout<<'-';
map<string,string>::iterator it;
for(it=m.begin();it!=m.end();it++)
{
if(it==m.begin())
cout<<it->first;
else
cout<<','<<it->first;
}
cout<<endl;
}
if(!p2.empty())
{
cout<<'*';
for(itt=p2.begin();itt!=p2.end();itt++)
{
if(itt==p2.begin())
cout<<*itt;
else
cout<<','<<*itt;
}
cout<<endl;
}
if(p1.empty()&&p2.empty()&&m.empty())
cout<<"No changes"<<endl;
cout<<endl;
} return ;
}

最新文章

  1. xcode 一般插件
  2. No module named &#39;urllib2&#39;
  3. Egit Patch
  4. AC日记——单词翻转 1.7 27
  5. zhx and contest (枚举  + dfs)
  6. discuz安装与学习资料
  7. FastDFS的安装配置
  8. TCP三次握手和四次挥手协议
  9. Android的Manifest配置文件介绍
  10. 线程:CyclicBarrier同步工具类
  11. hdu1881 毕业bg(深搜索dfs)
  12. JAVA实现网页上传头像
  13. 《PHP制作个人博客》之四:分类添加及前端导航数据用php动态调取
  14. Jmeter中基本操作
  15. Sitecore8.2 GeoIP - 在8.2的引擎盖下发生了什么?
  16. Zookeeper —— 初识
  17. 奇怪吸引子---AnishchenkoAstakhov
  18. 稀疏傅里叶变换(sparse FFT)
  19. Java bean中布尔类型使用注意
  20. CF 1051 G. Distinctification

热门文章

  1. Intellij IDEA 快捷键 与 环境设置
  2. sparkRDD:第3节 RDD常用的算子操作
  3. 学习java时在要求输出的数字带俩个小数点时,利用String.format时出现的问题
  4. 61二叉搜索树的第k个结点
  5. LeetCode 101.对称二叉树 - JavaScript
  6. List模拟栈
  7. PLSQL Developer常用设置及快捷键
  8. Django线上部署代码修改失效问题
  9. 一 SpringMvc概述&amp;入门配置
  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:基本的表格