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& ...
随机推荐
- 通过命令行提交更新代码到gitlab上
解决方法: 1.打开命令行的窗口,定位到项目所在的路径. 2.输入:git status,敲回车查看代码是否有更新,有更新的话会出现文件改变的文件名.(红色的) 3.输入:git commit -a ...
- 58按之字形顺序打印二叉树 +队列访问使用front和back,栈才是top
题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 思路:最暴力的方法就是使用队列进行层次遍 ...
- 在java中调用python方法
1.http://sourceforge.net/projects/jython/下载jython包,把其中的jython.jar添加到工程目录 示例: 1.摘自:http://blog.csdn.n ...
- Xcode下载途径
Xcode除了能在Appstore直接下载外,还可以用开发者账号登陆开发者中心[https://developer.apple.com/download/]下载对应的资源. 在开发者中心下载的好处是, ...
- 集合set 1
集合只能通过set() 函数进行创建 无序,不重复 每个元素必须是可哈希的,不可变类型(不可变数据类型在第一次声明赋值声明的时候, 会在内存中开辟一块空间, 用来存放这个变量被赋的值, 而 ...
- spark广播变量定时更新
广播变量 先来简单介绍下spark中的广播变量: 广播变量允许程序员缓存一个只读的变量在每台机器上面,而不是每个任务保存一份拷贝.例如,利用广播变量,我们能够以一种更有效率的方式将一个大数据量输入集合 ...
- P1095 解码PAT准考证
1095 解码PAT准考证 (25分) PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: ...
- SpringAOP原理分析
目录 Spring核心知识 SpringAOP原理 AOP编程技术 什么是AOP编程 AOP底层实现原理 AOP编程使用 Spring核心知识 Spring是一个开源框架,Spring是于2003年兴 ...
- 使用redis集群中遇到的错误
一. 上述错误的原因: 1.在redis服务器上关闭防火墙 2.可能是host写错了 上述错误的原因: 配置文件中jedisClient代表的是单机版的redis,但是在类中转化的时候转化的是集群版
- Linux之文件传输
本文借鉴<Linux命令大全> 1. bye命令 功能:终端FTP连线并结束程序 语法:bye 补充:在ftp模式下,输入bye即可中断目前的连线作业,并结束ftp的执行. 2. ftp命 ...