Problem C Updating a Dictionary
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 ;
}
Problem C Updating a Dictionary的更多相关文章
- csuoj 1113: Updating a Dictionary
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 1113: Updating a Dictionary Time Limit: 1 Sec ...
- [ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]
Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, where keys ...
- 湖南生第八届大学生程序设计大赛原题 C-Updating a Dictionary(UVA12504 - Updating a Dictionary)
UVA12504 - Updating a Dictionary 给出两个字符串,以相同的格式表示原字典和更新后的字典.要求找出新字典和旧字典的不同,以规定的格式输出. 算法操作: (1)处理旧字典, ...
- [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary
题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...
- CSU 1113 Updating a Dictionary
传送门 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Description In th ...
- Updating a Dictionary UVA - 12504
In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...
- CSU 1113 Updating a Dictionary(map容器应用)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...
- Uva 511 Updating a Dictionary
大致题意:用{ key:value, key:value, key:value }的形式表示一个字典key表示建,在一个字典内没有重复,value则可能重复 题目输入两个字典,如{a:3,b:4,c: ...
- Uva - 12504 - Updating a Dictionary
全是字符串相关处理,截取长度等相关操作的练习 AC代码: #include <iostream> #include <cstdio> #include <cstdlib& ...
随机推荐
- js运动框架及应用
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- luogu P2280 激光炸弹(二维前缀和)
由题给的xi, yi范围,可以建立二维地图maze[i][j],记录i j范围上的所有目标的价值总和 即有maze[xi][yi] += wi 然后接下来的目标就是求出该二维数组的前缀和 可得到前缀和 ...
- Linux centosVMware 配置Tomcat监听80端口、配置Tomcat虚拟主机、Tomcat日志
一.配置Tomcat监听80端口 关闭tomcat报错 [root@davery src]# /usr/local/tomcat/bin/shutdown.sh 重装tomcat即可 vim /usr ...
- idea右键新建选项没有类和包的创建方式
Intelidea创建好项目之后,右键新建Java class的时候发现没有改选项,只有以下几个选项 把sec目录设为源码目录,首先打开Project Structure
- HTML5画的简单时钟
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C语言笔记 14_标准库&assert&ctype&errno&float&limits
C 标准库 <assert.h> 简介 C 标准库的 assert.h头文件提供了一个名为 assert 的宏,它可用于验证程序做出的假设,并在假设为假时输出诊断消息. 已定义的宏 ass ...
- Numpy中 arange() 的用法
1. 概述Numpy 中 arange() 主要是用于生成数组,具体用法如下: 2. arange()2.1 语法numpy.arange(start, stop, step, dtype = Non ...
- ImageMagick 将PDF转图片命令
将 pdf 转一张图片 PS C:\Users\Microestc\desktop> magick convert -density -quality .pdf -append .jpeg ro ...
- 自定义autograd
这个操作博主认为应该是判断其requires_grad是否为True,从而判断是否进行梯度的运算但是至于这个为啥这么写,博主也不是很清楚 device = torch.device('cuda' if ...
- Netty实现原理和使用
参考: https://www.jdon.com/concurrent/netty.html Java NIO原理和使用 参考:https://www.jdon.com/concurrent/nio% ...