【UVA】12504 - Updating a Dictionary(map,string,vector模拟)

一般的模拟题,一开始WA,可能是用string的容器放char,改成string就过了

14073581 12504 Updating a Dictionary Accepted C++ 0.032 2014-08-21 07:12:19

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<string>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define INF (1 << 10)
#define esp 1e-9
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================
===========================================*/
#define MAXD 100 + 10
map<string,string>pre;
map<string,string>now;
vector<string>add;
vector<string>cost;
vector<string>change;
vector<string>_str1;
vector<string>_str2;
void init(){
    pre.clear();
    now.clear();
    add.clear();
    cost.clear();
    change.clear();
    _str1.clear();
    _str2.clear();
}
void solve1(string str){
    int L = str.size();
    string key = "",value = "";
    int ok = 0;
    for(int i = 1 ; i < L - 1; i++){
         if(str[i] == ':'){
             _str1.push_back(key);
             ok = 1;
         }
         else if(str[i] == ','){
             pre[key] = value;
             value = "";
             key = "";
             ok = 0;
         }
         else if(!ok)
            key += str[i];
         else
            value += str[i];
    }
    pre[key] = value;
}
void solve2(string str){
    int L = str.size();
    string key = "",value="";
    int ok = 0;
    for(int i = 1 ; i < L - 1; i++){
         if(str[i] == ':'){
             _str2.push_back(key);
             ok = 1;
         }
         else if(str[i] == ','){
             now[key] = value;
             value = "";
             key = "";
             ok = 0;
         }
         else if(!ok)
            key += str[i];
         else
            value += str[i];
    }
    now[key] = value;
}
void solve(){
    for(int i = 0 ; i < _str2.size() ; i++){
        string str = _str2[i];  /*在新字典里面找*/
        //cout << str <<" " <<  now[str] << endl;
        if(!pre.count(str))    /*旧字典没有这个*/
            add.push_back(str);
        else if(pre[str] != now[str])
            change.push_back(str);
    }
    for(int i = 0 ; i < _str1.size() ; i++){
        string str = _str1[i];  /*在旧字典里面找*/
        //cout << str <<" " << pre[str] << endl;
        if(!now.count(str))     /*新字典没有这个*/
            cost.push_back(str);
    }
}
int main(){
    int T;
    cin >> T;
    while(T--){
        init();
        string str;
        cin >> str;
        solve1(str);
        cin >> str;
        solve2(str);
        solve();
        if(!add.size() && !cost.size() && !change.size())
            cout << "No changes" << endl;
        else{
            if(add.size()){
                printf("+");
                sort(add.begin(),add.end());
                for(int i = 0 ; i < add.size() ; i++){
                    cout << add[i];
                    if(i < add.size() - 1)
                    cout << ",";
                    else
                    cout << endl;
                }
            }
            if(cost.size()){
                printf("-");
                sort(cost.begin(),cost.end());
                for(int i = 0 ; i < cost.size() ; i++){
                    cout << cost[i];
                    if(i < cost.size() - 1)
                    cout << ",";
                    else
                    cout << endl;
                }
            }
            if(change.size()){
                printf("*");
                sort(change.begin(),change.end());
                for(int i = 0 ; i < change.size() ; i++){
                    cout << change[i];
                    if(i < change.size() - 1)
                    cout << ",";
                    else
                    cout << endl;
                }
            }
        }
        cout << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值