Uva12504 Updating a Dictonary
这道题难度不大,主要是考察熟练运用C++的容器,字符串等操作。
另外注意特殊情况是否需要特殊处理。即当一个字典为空时,无论另一个字典是否有值,输出的结果都为No Change,这点需要注意一下。
另外,仔细阅读题目的输入输出部分,避免定势思维而误解题意,比如这题我还以为不用输出最后一个数据的空行,其实题目说的是每个数据后都有一空行。
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <sstream>
using namespace std;
map<string,string> map1;
map<string,string> map2;
int main(){
int n;
string s,s3;
string dc(",:{}");
cin >> n;
while(n--){
string::size_type pos=;;
cin >> s;
set<string> set1;//+
set<string> set2;//-
set<string> set3;//*
map1.clear();
map1[","]=",";
while((pos=s.find_first_of(dc,pos))!=string::npos){
s.replace(pos,," ");
pos++;
}
string buf;
stringstream ss(s);
string s1,s2;
while(ss>>buf){
if(s1.empty())s1=buf;
else s2=buf;
if(!s2.empty()){
map1[s1]=s2;
s1=s2="";
}
}
/*
for(auto a : map1){
cout << a.first << " " << a.second <<endl;
}
*/
cin >> s3; map2.clear();
map2[","]=",";
pos=; while((pos=s3.find_first_of(dc,pos))!=string::npos){
s3.replace(pos,," ");
pos++;
}
stringstream ss1(s3);
s1=s2="";
while(ss1>>buf){
if(s1.empty())s1=buf;
else s2=buf;
if(!s2.empty()){
map2[s1]=s2;
s1=s2="";
}
}
/*
for(auto a : map2){
cout << a.first << " " << a.second <<endl;
}
*/
for(auto a : map1){
for(auto b : map2){
if(! map1[b.first].empty()){
if(map1[b.first]!=b.second){
//此处表示修改
set3.insert(b.first);
}
}
else {
set1.insert(b.first);
//表示新增
}
}
}
for(auto a : map2){
for(auto b : map1){
if(map2[b.first].empty())
{
set2.insert(b.first);
//表示新增
}
}
}
bool is_changed1=,is_changed2=,is_changed3=;
for(set<string>::iterator it = set1.begin();it!=set1.end();it++){
if((*it).empty())continue;
if(it!=set1.begin())cout << ",";
else cout << "+";
cout << *it;is_changed1=;
}
if(is_changed1)cout <<endl;
for(set<string>::iterator it = set2.begin();it!=set2.end();it++){
if((*it).empty())continue;
if(it!=set2.begin())cout << ",";
else cout << "-";
cout << *it;is_changed2=;
}
if(is_changed2)cout <<endl;
for(set<string>::iterator it = set3.begin();it!=set3.end();it++){
if((*it).empty())continue; if(it!=set3.begin())cout << ",";
else cout << "*";is_changed3=;
cout << *it;
}
if(is_changed3)cout <<endl;
if(is_changed1+is_changed2+is_changed3==)cout <<"No changes" <<endl; cout <<endl;
}
return ;
}
代码目前比较繁琐,日后再简化一下代码。
Uva12504 Updating a Dictonary的更多相关文章
- 湖南生第八届大学生程序设计大赛原题 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 ...
- 更新字典 (Updating a Dictionary,UVa12504)
题目描述: 解题思路: 1.根据:和,获得字符串 2.使用两个map进行比较: #include <iostream> #include <algorithm> #includ ...
- CocoaPods被卡住:Updating local specs repositories
使用CocoaPods被卡住:Updating local specs repositories 使用 pod install --verbose --no-repo-update
- 使用CocoaPods被卡住:Updating local specs repositories
使用cocoapods 更新第三库,一直停留在.Updating local specs repositories 后来查发现pod install 被墙了,请大家换成pod install --v ...
- MySQL提示:The server quit without updating PID file问题的解决办法(转载)
MySQL提示:The server quit without updating PID file问题的解决办法 今天网站web页面提交内容到数据库,发现出错了,一直提交不了,数找了下原因,发现数据写 ...
- Mysql 启动错误:the server quit without updating pid
接到任务看看mysql为啥起不来,就上服务器上看了看,确实起不来,至于之前发生了啥也不知道. 服务器Ubuntu,mysql-5.6 1.先试下mysql登陆 mysql -uroot -p 发现报错 ...
- lnmp下启动mysql报错 The server quit without updating PID file
启动时候错误代码:Starting MySQL[FAIL.] The server quit without updating PID file (/var/run/mysqld/mysqld.pid ...
- MySQL数据库出现The server quit without updating PID file.
一.服务器环境 操作系统:CentOS-6.4 服务器环境:集成环境LNMP1.0 二.步骤重现 1.安装LNMP1.0,具体操作方法见这里,安装成功: 2.因个人需求,现将MySQL数据库存放在/d ...
随机推荐
- [Flex] PopUpButton系列 —— 设置弹出菜单与主按钮之间的间隔
<?xml version="1.0" encoding="utf-8"?><!--设置弹出菜单与主按钮之间的间隔 PopUpButtonPo ...
- 可以获取客户端ip的API
http://www.ip138.com/ip2city.asp http://www.bliao.com/ip.phtml http://www.whereismyip.com/ http://ww ...
- [HDU 4787] GRE Words Revenge (AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4787 题目大意: 给你若干个单词,查询一篇文章里出现的单词数.. 就是被我水过去的...暴力重建AC自 ...
- XML文件操作类--创建XML文件
这个类是在微软XML操作类库上进行的封装,只是为了更加简单使用,包括XML类创建节点的示例. using System; using System.Collections; using System. ...
- C和C++头文件的不同
#include <IOSTREAM.h>void main(){ std::cout<<"Hello,World!"<<std::end ...
- java.io.Serializable浅析
转自:http://www.cnblogs.com/gw811/archive/2012/10/10/2718331.html Java API中java.io.Serializable接口源码: p ...
- JMeter二次开发(2)-编写 JSON Assertion 插件
本篇文章主要介绍如何对JMeter进行二次开发,添加自己所需的功能.这里以Json验证为例进行说明.在web接口测试过程中,JSON的应用已经非常普遍,但原声的JMeter并没有提供Json及Json ...
- 一步一步学习Unity3d学习笔记系1.4单服模式架构
单服模式更适合做手游,只有一个服务器,在程序中通过代码模块来实现各功能,而不是物理模块划分. 登录模块实现,账号数据处理, 用户模块,处理角色权限处理, 匹配模块,匹配战斗 好友模块,负责好友管理 战 ...
- Modifiers
Sometimes it is useful for a function to modify the objects it gets as parameters. In that case, the ...
- 优秀的前端上传文件插件 web uploader
WebUploader是由Baidu WebFE(FEX)团队开发的一个简单的以HTML5为主,FLASH为辅的现代文件上传组件.在现代的浏览器里面能充分发挥HTML5的优势,同时又不摒弃主流IE浏览 ...