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& ...
随机推荐
- idea 编译maven
参考:https://blog.csdn.net/yye894817571/article/details/71681891
- 各颜色LED压降
一下是参考1.直插LED压降红:2.0-2.2V黄:1.8-2.0V绿:3.0-3.2V 额定电流约20mA.2.贴片LED压降红:1.82-1.88V,电流5-8mA绿:1.75-1.82V,3-5 ...
- SRS源码——UDP
srs_app_server.cpp int SrsServer::listen() { int ret = ERROR_SUCCESS; if ((ret = listen_rtmp()) != ...
- 「luogu4366」最短路
「luogu4366」最短路 传送门 直接连边显然不行,考虑优化. 根据异或的结合律和交换律等优秀性质,我们每次只让当前点向只有一位之别的另一个点连边,然后就直接跑最短路. 注意点数会很多,所以用配对 ...
- 115、Java中String类之使用concat进行字符串连接
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- Java程序生成exe可执行文件
Java程序打包成exe可执行文件,分为两大步骤. 第一步:将Java程序通过Eclipse或者Myeclipse导成Jar包 第二步:通过exe4j讲Jar包程序生成exe可执行文件 第一步详解: ...
- Xcode下载途径
Xcode除了能在Appstore直接下载外,还可以用开发者账号登陆开发者中心[https://developer.apple.com/download/]下载对应的资源. 在开发者中心下载的好处是, ...
- USACO January Contest Gold Time is Mooney 题解
题意 给出一个有向图,走到每个节点有 \(m_i\) 的收益,每一条边要走一天,走 \(T\) 天的花费是 \(C\cdot T^2\),求从节点 \(1\) 开始并且在节点 \(1\) 结束的旅行的 ...
- Python 中的经典类新式类
Python 中的经典类新式类 要知道经典类和新式类的区别,首先要掌握类的继承 类的继承的一个优点就是减少代码冗余 广度优先和深度优先,这主要是在多类继承的时候会使用到 经典类和新式类的主要区别就是类 ...
- C 随机数产生
// ConsoleApplication5.cpp : Defines the entry point for the console application. // #include " ...