Uva12504 Updating a Dictonary

  这道题难度不大,主要是考察熟练运用C++的容器,字符串等操作。

  另外注意特殊情况是否需要特殊处理。即当一个字典为空时,无论另一个字典是否有值,输出的结果都为No Change,这点需要注意一下。

  另外,仔细阅读题目的输入输出部分,避免定势思维而误解题意,比如这题我还以为不用输出最后一个数据的空行,其实题目说的是每个数据后都有一空行。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <map>
  4 #include <set>
  5 #include <sstream>
  6 using namespace std;
  7 map<string,string> map1;
  8 map<string,string> map2;
  9 int main(){
 10     int n;
 11     string s,s3;
 12     string dc(",:{}");
 13     cin >> n;
 14     while(n--){
 15         string::size_type pos=0;;
 16         cin >> s;
 17         set<string> set1;//+
 18         set<string> set2;//-
 19         set<string> set3;//*
 20         map1.clear();
 21         map1[","]=",";
 22         while((pos=s.find_first_of(dc,pos))!=string::npos){
 23             s.replace(pos,1," ");
 24             pos++;
 25         }
 26         string buf;
 27         stringstream ss(s);
 28         string s1,s2;
 29         while(ss>>buf){
 30             if(s1.empty())s1=buf;
 31             else s2=buf;
 32             if(!s2.empty()){
 33                 map1[s1]=s2;
 34                 s1=s2="";
 35             }
 36         }
 37         /*
 38         for(auto a : map1){
 39             cout << a.first << " " << a.second <<endl;
 40         }
 41         */
 42         cin >> s3;
 43
 44         map2.clear();
 45         map2[","]=",";
 46         pos=0;
 47
 48         while((pos=s3.find_first_of(dc,pos))!=string::npos){
 49             s3.replace(pos,1," ");
 50             pos++;
 51         }
 52         stringstream ss1(s3);
 53         s1=s2="";
 54         while(ss1>>buf){
 55             if(s1.empty())s1=buf;
 56             else s2=buf;
 57             if(!s2.empty()){
 58                 map2[s1]=s2;
 59                 s1=s2="";
 60             }
 61         }
 62         /*
 63         for(auto a : map2){
 64             cout << a.first << " " << a.second <<endl;
 65         }
 66         */
 67         for(auto a : map1){
 68             for(auto b : map2){
 69                 if(! map1[b.first].empty()){
 70                     if(map1[b.first]!=b.second){
 71                         //此处表示修改
 72                         set3.insert(b.first);
 73                     }
 74                 }
 75                 else {
 76                     set1.insert(b.first);
 77                     //表示新增
 78                 }
 79             }
 80         }
 81         for(auto a : map2){
 82             for(auto b : map1){
 83                 if(map2[b.first].empty())
 84                 {
 85                     set2.insert(b.first);
 86                     //表示新增
 87                 }
 88             }
 89         }
 90         bool is_changed1=0,is_changed2=0,is_changed3=0;
 91         for(set<string>::iterator it = set1.begin();it!=set1.end();it++){
 92             if((*it).empty())continue;
 93             if(it!=set1.begin())cout << ",";
 94             else cout << "+";
 95             cout << *it;is_changed1=1;
 96         }
 97         if(is_changed1)cout <<endl;
 98         for(set<string>::iterator it = set2.begin();it!=set2.end();it++){
 99             if((*it).empty())continue;
100             if(it!=set2.begin())cout << ",";
101             else cout << "-";
102             cout << *it;is_changed2=1;
103         }
104         if(is_changed2)cout <<endl;
105         for(set<string>::iterator it = set3.begin();it!=set3.end();it++){
106             if((*it).empty())continue;
107
108             if(it!=set3.begin())cout << ",";
109             else cout << "*";is_changed3=1;
110             cout << *it;
111         }
112         if(is_changed3)cout <<endl;
113         if(is_changed1+is_changed2+is_changed3==0)cout <<"No changes" <<endl;
114
115         cout <<endl;
116     }
117     return 0;
118 }

代码目前比较繁琐,日后再简化一下代码。

时间: 2024-08-06 07:56:33

Uva12504 Updating a Dictonary的相关文章

更新字典 (Updating a Dictionary,UVa12504)

题目描述: 解题思路: 1.根据:和,获得字符串 2.使用两个map进行比较: 1 #include <iostream> 2 #include <algorithm> 3 #include <string> 4 #include <map> 5 using namespace std; 6 map<string, string>::iterator it; 7 map<string,string> dict[2]; 8 string

mysql编译安装完成后,启动时报错The server quit without updating PID file

源码包下载: 编译安装: 设置配置文件: 添加启动脚本: 添加开机启动项: 完成后,/etc/init.d/mysqld start 时候报错如下: Starting MySQL..The server quit without updating PID file ( [FAILED] al/mysql/data/FrankTest01.pid). 1.防火墙SELINUX有没有关闭 2.3306端口有没有被占用   netstat -tulanp 3.是否有其他相关的mysqld进程在运行.

解决Linux下MySQL启动错误Starting MySQL.Manager of pid-file quit without updating file.[FAILED]

刚装完MySQL,启动时报如下错误: Starting MySQL.Manager of pid-file quit without updating file.[FAILED] 依次尝试了从谷歌找到的各种方法,发现只要执行如下MySQL初始化命令即可解决: /usr/local/mysql/bin/mysql_install_db --user=mysql 下面附上从谷歌找到的解决此问题的方法集锦,也许每个人遇到的问题的原因会不一样,经供参考: 一.查看报错日志 报错日志是我们解决问题的方向灯

开启mysql慢查询,重启提示Starting MySQL.The server quit without updating PID file

在my.cnf文件中加入: datadir = /usr/local/mysql/data long_query_time = 1 #记录下查询时间查过1秒. log-slow-queries = /usr/local/mysql/log/db-Test2-slow.log #日志目录. #log-queries-not-using-indexes #表示记录下没有使用索引的查询. 之后,重启mysql,提示: [[email protected] subsys]# service mysql 

svn: E155017: Checksum mismatch while updating 校验错误的解决方法

[10.3.53.53:/EMRCV5]# svn up svn: E155004: Working copy '/EMRCV5' locked. svn: E155004: '/EMRCV5' is already locked. svn: run 'svn cleanup' to remove locks (type 'svn help cleanup' for details) [10.3.53.53:/EMRCV5]# svn cleanup [10.3.53.53:/EMRCV5]#

the server quit without updating pid file (/var/lib/mysql/localhost.localdomain.pid)

前几天装的mysql,用的还挺爽的,第二天再用就不行了,报的错误如标题.网上也是众说纷纭,可能有很多原因会导致这种错误吧.我用的是将Mysqld这个进程杀掉,就可以启动mysql了 the server quit without updating pid file (/var/lib/mysql/localhost.localdomain.pid),布布扣,bubuko.com the server quit without updating pid file (/var/lib/mysql/l

MySQL提示:The server quit without updating PID file问题的解决办法

今天网站web页面提交内容到数据库,发现出错了,一直提交不了,数找了下原因,发现数据写不进去!第一反应,重启mysql数据库,一直执行中,停止不了也启动不了,直觉告诉我磁盘满了 ! 用df命令查了下,果然磁盘满了,因为当时分区采用系统默认,不知道为什么不能自动扩容!以后在处理这个问题!如图所示: 复制代码 代码如下: [[email protected] ~]# df文件系统                 1K-块      已用      可用 已用% 挂载点/dev/mapper/vg_s

解决mysql启动时报The server quit without updating PID file 的错误(转)

1.一般是权限问题,把用户和组改为mysql就可以了. chown -R mysql:mysql  /var/lib/mysql 2.在启动mysql时报下列错误 [[email protected] ~]# service mysql.server startStarting MySQL......................................The server quit without updating PID file (/var/lib/mysql/mysqld2.c

Elasticsearch Updating documents

在ES裡, 更新documents到index不是一件簡單的事情, 我們得在這裡留個心眼. 眾所皆知RESTful風格使用HTTP的動詞來進行所謂的Update, ES也是如此. 要注意到ES的更新有兩種方式 整個docment更新(取代,覆蓋) 根據某個field更新(count += 3, append字符串, 替數組增加元素, 刪除field) //根據某個field更新$ curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "s