题目链接: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容器应用)的更多相关文章

  1. CSU 1113 Updating a Dictionary

    传送门 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Description In th ...

  2. csuoj 1113: Updating a Dictionary

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 1113: Updating a Dictionary Time Limit: 1 Sec  ...

  3. HDU 1113 Word Amalgamation (map 容器 + string容器)

    http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across ...

  4. 湖南生第八届大学生程序设计大赛原题 C-Updating a Dictionary(UVA12504 - Updating a Dictionary)

    UVA12504 - Updating a Dictionary 给出两个字符串,以相同的格式表示原字典和更新后的字典.要求找出新字典和旧字典的不同,以规定的格式输出. 算法操作: (1)处理旧字典, ...

  5. [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary

    题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...

  6. Problem C Updating a Dictionary

    Problem C     Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, ...

  7. map 容器的使用

    C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明    1   头文件   #include   <map> ...

  8. 一种map容器遍历的方法

    遍历算法是一种很常见而且非常重要的算法,我们用map容器的时候可能用的比较多的是查找,我今天才第一次要用到遍历.下面举个例子就知道了. map<string,string> mp; str ...

  9. map容器

    map容器一般用于对字符串进行编号,主要用于建图方面,例如把城市名按数字进行编号 #include"stdio.h" #include"string.h" #i ...

随机推荐

  1. java函数substring()

    String str; str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str: str=str.sub ...

  2. mysql support chinese

      1.创建table的时候使用utf8编码 create table tablename ( id int NOT NULL, content var_char(250) NOT NULL, CON ...

  3. jquery 图片浏览功能实现

    效果展示: HTML代码: <div id="no3"> <img src="./img/last.png" id="last&qu ...

  4. C#--使用存储过程

    过程: 1.连接数据库 2.设置执行类型为存储过程,区别于SQL语句 3.得到执行结果,返回值.输出参数等 //连接数据库 string connstr = "Data Source=ZGC ...

  5. fileinput模块

    刚才练习的时候,报如下错误: AttributeError: module 'fileinput' has no attribute 'input',后来Google参考这篇文章https://mai ...

  6. Python + OpenCV2 系列:3 - python 字符串,类,编码规范

    首先,强烈推荐<<简明 Python 教程>> Swaroop, C. H. 著 沈洁元 译 其实,这本书里已经把python的最基本的用法,编码等等介绍的很好,这里把我用到的 ...

  7. ecshop 获取某个商品的 所有订单信息 或者销量

    把一下代码放到 lib_main.php 1.统计某个下单商品的人数 function get_goods_ordernum($goods_id){ $sql = "select count ...

  8. super的作用(转自百度)

    super出现在继承了父类的子类中.有三种存在方式:第一种super.xxx;(xxx为变量名或对象名)这种方法意义为,获取父类中的名字为xxx的变量或方法引用.使用这种方法可以直接访问父类中的变量或 ...

  9. webapi输入验证过滤器ValidationActionFilter

    public class validationActionFilter:ActionFilterAttribute { public override void OnActionExecuting(S ...

  10. linq按条件sum

    var result = fruit.GroupBy(i => i.date) .Select(i => new { date = i.Key, no_of_apple = i.Where ...