算法竞赛入门经典 第二版 习题5-11 更新字典 Updating a Dictionary uva12504

题目:https://vjudge.net/problem/UVA-12504


思路:用集合的思想,旧字典与新字典的差集就是减少部分,新字典与旧字典的差集就是增加部分,新旧字典的交集中键值改变的部分就是修改部分。

注:读入时别忘单独处理空集。


代码:C++11


#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

map<string, string> oridata;
map<string, string> newdata;
set<string> orikey;
set<string> newkey;
set<string> increase;
set<string> decrease;
set<string> change;

void getdata(map<string, string> &nowdata, set<string> &nowkey)
{
    bool keyflag = true;
    getchar();
    string key, value;
    while(1)
    {
        char c;
        c = getchar();
        if(c=='}')
        {
            if(keyflag)
            {
                getchar();
                return ;
            }
            nowdata[key] = value;
            nowkey.insert(key);
            getchar();
            return ;
        }
        else if(c==':')
        {
            keyflag = !keyflag;
        }
        else if(c==',')
        {
            keyflag = !keyflag;
            nowdata[key] = value;
            nowkey.insert(key);
            key.clear();
            value.clear();
        }
        else if(keyflag)
        {
            key.push_back(c);
        }
        else
        {
            value.push_back(c);
        }
    }
}

void print(set<string> key)
{
    bool first = true;
    for(auto &t:key)
    {
        if(first)
        {
            first = false;
        }
        else
        {
            printf(",");
        }
        cout << t << flush;
    }
    cout << endl;
}

int main()
{
    int T;
    cin >> T;
    getchar();
    while(T--)
    {
        oridata.clear();
        newdata.clear();
        orikey.clear();
        newkey.clear();
        increase.clear();
        decrease.clear();
        change.clear();
        getdata(oridata, orikey);
        getdata(newdata, newkey);
        set_difference(newkey.begin(), newkey.end(), orikey.begin(), orikey.end(), inserter(increase, increase.begin()));
        set_difference(orikey.begin(), orikey.end(), newkey.begin(), newkey.end(), inserter(decrease, decrease.begin()));
        set<string> intersection;
        set_intersection(orikey.begin(), orikey.end(), newkey.begin(), newkey.end(), inserter(intersection, intersection.begin()));
        for(auto &t: intersection)
        {
            if(oridata[t]!=newdata[t])
            {
                change.insert(t);
            }
        }
        if(increase.empty()&&decrease.empty()&&change.empty())
        {
            cout << "No changes" << endl;
        }
        if(!increase.empty())
        {
            printf("+");
            print(increase);
        }
        if(!decrease.empty())
        {
            printf("-");
            print(decrease);
        }
        if(!change.empty())
        {
            printf("*");
            print(change);
        }
        cout << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值