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& ...
 
随机推荐
- 【剑指Offer面试编程题】题目1362:左旋转字符串--九度OJ
			
题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=&qu ...
 - Maven 项目中使用 logback
			
添加依赖 <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logsta ...
 - linux 部署java 项目命令
			
1:服务器部署路径:/home/tomcat/tomcat/webapps (用FTP工具链接服务器把包上传到此目录) 2:进入项目文件夹 cd /home/tomcat/tomcat/webapp ...
 - CSS3-边框(border-radius、box-shadow、border-image)
			
CSS3中的边框属性:border-radius.box-shadow.border-image 圆角:border-radius 使用 CSS3 border-radius 属性,你可以给任何元素制 ...
 - JS回弹原理-高级
			
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
 - 设计模式课程 设计模式精讲 13-2 享元模式coding
			
1 代码演练 1.1 代码演练1 1 代码演练 1.1 代码演练1 需求: 每周由随机部门经历做报告: 重点关注: a 该案例是单例模式和享元模式共同使用 b 外部传入的department是外部状态 ...
 - PLSQL Developer常用设置及快捷键
			
CSDN日报20170314--<40岁程序员真的要被淘汰了么?> 程序员2月书讯 [直播]用面向协议的思想简化网络请求 博客一键搬家活动开始啦 PLSQL Developer常用设置及快 ...
 - 1-1SpringBoot简介
			
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...
 - Spark教程——(2)编写spark-submit测试Demo
			
创建Maven项目: 填写Maven的pom文件如下: <?xml version="1.0" encoding="UTF-8"?> <pro ...
 - div  浮动
			
浮动 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <titl ...