CSU 1113 Updating a Dictionary(map容器应用)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113
解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为一个关键字对应一个值,输入格式如下:
{a:3,b:4,c:10,f:6}
{a:3,c:5,d:10,ee:4}
冒号前面的表示关键字,冒号后面的数字表示值,关键字由小写字母组成。
现在让你判断,如果新的字典相对于原来的字典有新增的关键字以以下格式输出 :+key1,key2,....
如果新的字典相对于原来的字典少了关键字以以下格式输出:-key1,key2....
如果新的字典的关键字对于原来的字典的相同的关键字对应的值有变化以以下格式输出:*key1,key2...
主要有两个过程,首先把输入拆分成一个一个元素的形式存入map容器,然后就是比较的过程,这个简单了。要注意的是同一个关键字可能输入多次,所以要注意去重。然后还有据说这个题目的输入有问题,输入的时候有两个换行符,不过你只要都用scanf输入就行了,不用管。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<deque>
#include<queue>
#include<cstdlib>
#include<string>
#include<map>
using namespace std;
const int maxn = ;
map<string,string> old,New; char str[][maxn];
int changeto(map<string,string>& mp,char* str)
{
string a = "",b = "";
int len = strlen(str),f = ;
for(int i = ;i < len;++i)
{
if(str[i] == '{' || str[i] == ',' || str[i] == '}')
{
mp.insert(pair<string,string> (a,b));
a = "";
b = "";
continue;
}
if(str[i] >= 'a' && str[i] <= 'z')
a += str[i];
else if(str[i] >= '' && str[i] <= '')
b += str[i];
}
return f;
}
int main()
{
int T,flag = ;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",str[],str[]);
int num1 = changeto(old,str[]);
int num2 = changeto(New,str[]);
int j,ff = ,fff = ;
for(map<string,string>::iterator iter = New.begin();iter != New.end();++iter)
{
if(old.insert(*iter).second == )
{
printf(ff? ",%s":"+%s",iter->first.c_str());
old.erase(iter->first); // 插入成功的话一定要记得删除,不然会影响后面的查找
ff = ;
}
}
if(ff) puts("");
fff = max(ff,fff);
ff = ;
for(map<string,string>::iterator iter = old.begin();iter != old.end();++iter)
{
if(New.insert(*iter).second == )
{
printf(ff? ",%s":"-%s",iter->first.c_str());
New.erase(iter->first);
ff = ;
}
}
if(ff) puts("");
fff = max(ff,fff);
ff = ;
for(map<string,string>::iterator iter = New.begin();iter != New.end();++iter)
{
if(old.insert(*iter).second == && old[iter->first] != New[iter->first])
{
printf(ff? ",%s":"*%s",iter->first.c_str());
ff = ;
}
}
if(ff) puts("");
fff = max(ff,fff);
if(fff == ) puts("No changes");
puts("");
old.clear();
New.clear();
}
return ;
}
CSU 1113 Updating a Dictionary(map容器应用)的更多相关文章
- CSU 1113 Updating a Dictionary
传送门 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Description In th ...
- csuoj 1113: Updating a Dictionary
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 1113: Updating a Dictionary Time Limit: 1 Sec ...
- HDU 1113 Word Amalgamation (map 容器 + string容器)
http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across ...
- 湖南生第八届大学生程序设计大赛原题 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 ...
- Problem C Updating a Dictionary
Problem C Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, ...
- map 容器的使用
C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明 1 头文件 #include <map> ...
- 一种map容器遍历的方法
遍历算法是一种很常见而且非常重要的算法,我们用map容器的时候可能用的比较多的是查找,我今天才第一次要用到遍历.下面举个例子就知道了. map<string,string> mp; str ...
- map容器
map容器一般用于对字符串进行编号,主要用于建图方面,例如把城市名按数字进行编号 #include"stdio.h" #include"string.h" #i ...
随机推荐
- HDU1392Surround the Trees(凸包判断 + 求周长)
http://www.cnblogs.com/hmhard/archive/2013/02/05/2893035.html 这是判断三角区域那块写的不好. 判断凸包的方法: 1.将所有点按照y从小到大 ...
- Diode -- Pay Attention to Parallel Connection
The above circuit is right. The two same resistors are integral. Because every diode is different, t ...
- C#------如何取出exe运行文件给客户使用
1.将解决方案配置里面的“Debug”转换成“Release” 2.右击“解决方案”,选着“重新生成解决方案”,以得到最新的版本 3.找到工程目录下的“bin”文件夹,里面有“Release”文件夹, ...
- linux安装pip
1.先说一下什么是pippip 是"A tool for installing and managing Python packages.",也就是说pip是python的软件安装 ...
- Linux下安装setup tools小工具
1, 最小化的linux系统(centos\redhat)默认都是没有安装setup图形小工具的,你输入setup命令会提示 command not found . 如果要使用这个命令安装方法 1.安 ...
- Android学习笔记——Handler(一)
使用Handler管理线程(转) 步骤: 1. 申请一个Handler对象 Handler handler = new Handler(); 2. 创建一个线程 {继承Thread类或者实现Runna ...
- Redis学习——SDS字符串源码分析
0. 前言 这里对Redis底层字符串的实现分析,但是看完其实现还没有完整的一个概念,即不太清楚作者为什么要这样子设计,只能窥知一点,需要看完redis如何使用再回头来体会,有不足之处还望告知. 涉及 ...
- ILMerge
ILMerge http://www.microsoft.com/en-hk/download/details.aspx?id=17630 ILMerge 下载地址:http://www.micros ...
- Class.forName()用法详解
Class.forName()用法详解 标签: classjvmjdbc数据库documentationjava 2012-03-29 09:39 40414人阅读 评论(8) 收藏 举报 分类: ...
- jQuery 鼠标拖拽移动窗口
拖拽移动需要注意的是:拖拽移动的窗口是如何定位的,如果"left"属性为"%" ,以"margin-left"来计算定位,如下实例,如果&q ...