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 }
复制代码

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

posted @   deepwzh  阅读(228)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示