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 ...
随机推荐
- 使用 Heka 导入自定义的nginx日志到Elasticsearch
重置Heka执行进度 heka的进度配置文件存在配置项 base_dir 设置的目录,只需要删除这个文件夹下面的内容,就可以完全重置heka的进度. base_dir 配置项默认是在下面目录: '/v ...
- ExtGrid
刷新表中数据 Ext.getCmp('SystemManage_role_ContainPresonnel_grid').store.reload(); store.load({ url: '/dat ...
- asp.net子窗体与父窗体交互
今天在项目上遇到了这个问题,其实只是window.returnValue的简单应用,不是asp.net的专属内容.作为积累,记录一个简单的实现模型. 图1 用到的文件 从图1中我们可以看到,只用到了 ...
- (easy)LeetCode 191.Number of 1 Bits
Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...
- java类包第十一章
1.同一个包中的类互相访问,不需要制定包名. 2.java中包名的规则是全部使用小写字母 3.final 方法不能被覆盖, public class OuterClass { innerClas ...
- 【转】android的一些开源项目
自己一直很喜欢Android开发,就如博客副标题一样,我想做个好的App. 在摸索过程中,GitHub上搜集了很多很棒的Android第三方库,推荐给在苦苦寻找的开发者,而且我会不定期的更新这篇文章. ...
- Oracle corrupt block(坏块) 详解
转自:http://blog.csdn.net/tianlesoftware/article/details/5024966 一. 坏块说明 1.1 相关链接 在看坏块之前,先看几个相关的链接,在后面 ...
- JAVA·多线程:创建线程的两种方式
1.–扩展java.lang.Thread类 package multiThread; public class Thread02extThread { public static void main ...
- DFS与BFS
顾名思义,DFS就是一直在一个方向搜索,当这一方向不可以时退回该点,换下一方向: 而BFS一开始就是向四面八方搜索,把符合条件的点存入队列中,当前一个点都搜索完毕时,再从队列顶中取出点,再向四面八方搜 ...
- 解决PHP生成校验码时“图像因其本身有错无法显示”的错误
今天使用http://crazymud.iteye.com/blog/452293给出的代码进行PHP生成校验码功能的实现,发现firefox一直提示“图像.......因其本身有错无法显示”的问题, ...