zoukankan      html  css  js  c++  java
  • 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 }

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

  • 相关阅读:
    android 回调的理解(结合接口)
    Android Bundle、Handler和Message类介绍
    Android: Intent实现活动之间的交互
    Condition实现一个生产者一个消费者
    condition实现通知部分线程
    Condition实现等待、通知
    ReentrantLock简单实现2
    ReentrantLock的简单使用
    线程通信-基于字符流管道
    线程通信-基于字节流管道
  • 原文地址:https://www.cnblogs.com/Wade-/p/6032334.html
Copyright © 2011-2022 走看看