【习题 5-11 UVA 12504 】Updating a Dictionary

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


不确定某个map里面是否有某个关键字的时候。
要用find来确定。
如果直接用访问下标的形式去做的话。
会强行给他加一个那个关键字(原来没有).
(当然那个关键字的映射为空就是了);

【代码】

#include <bits/stdc++.h>
using namespace std;

string s;
map<string, string> mmap1, mmap2;
vector <string> v[3];

void getmap(map<string, string> &mmap, string s)
{
    int len = s.size();
    for (int i = 0; i < len; i++)
        if (s[i] == '{' || s[i] == ':' || s[i] == ',' || s[i] == '}')
            s[i] = ' ';
    string a, b;
    stringstream ss(s);
    while (ss >> a >> b)
    {
        mmap[a] = b;
    }
}

int main()
{
    //freopen("F:\\rush.txt", "r", stdin);
    int unused;
    cin >> unused;
    while (unused--)
    {
        for (int i = 0; i < 3; i++)v[i].clear();
        mmap1.clear(); mmap2.clear();
        cin >> s;
        getmap(mmap1, s);
        cin >> s;
        getmap(mmap2, s);
        for (auto temp : mmap2)
            if (mmap1.find(temp.first)==mmap1.end()) v[0].push_back(temp.first);

        for (auto temp : mmap1)
            if (mmap2.find(temp.first)==mmap2.end()) v[1].push_back(temp.first);

        for (auto temp : mmap1)
            if (mmap2.find(temp.first)!=mmap2.end() && mmap2[temp.first] != temp.second)
            {
                string s1 = temp.second, s2 = mmap2[temp.first];
                v[2].push_back(temp.first);
            }
        bool ok = v[0].empty() && v[1].empty() && v[2].empty();
        if (ok) cout << "No changes" << endl;
        else
        {
            for (int i = 0; i < 3; i++)
                if (!v[i].empty())
                {
                    if (i == 0) cout << '+';
                    if (i == 1) cout << '-';
                    if (i == 2) cout << '*';
                    cout << v[i][0];
                    for (int j = 1; j < (int)v[i].size(); j++)
                        cout << "," << v[i][j];
                    cout << endl;
                }
        }

        cout << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值