紫书第五章习题 5-11 UVa 12504-updating a dictionary

出处:https://blog.csdn.net/acvay/article/details/43021077

题目链接:https://vjudge.net/contest/231030#problem/K

题意  比较两个字典  按字典序输出所有添加 删除 修改的项   如果没有任何更新  输出  No changes

STL map的应用  对比两个字典  注意开始字符串的处理和字典可以为空

    #include<bits/stdc++.h>  
    using namespace std;  
    map<string, string> d[2];  
    map<string, string>::iterator it;  
    const int N = 105;  
    string s, a, b, t[N];  
      
    void print(char c, int n)  
    {  
        sort(t, t + n), cout << c << t[0];  
        for(int i = 1; i < n; ++i) cout << ',' << t[i];  
        puts("");  
    }  
      
    int main()  
    {  
        int cas, n, c1, c2, c3;  
        cin >> cas;  
        while(cas--)  
        {  
            d[0].clear(), d[1].clear();  
            for(int i = 0; i < 2; ++i)  
            {  
                cin >> s;  
                int j = 1, l = s.size();  
                while(l > 2 && j < l)  
                {  
                    while(s[j] != ':') a += s[j++]; ++j;  
                    while(s[j] != ',' && s[j] != '}') b += s[j++]; ++j;  
                    d[i][a] = b, a = b = "";  
                    //cout << a << " : " << b << endl;  
                }  
            }  
      
            c1 = c2 = c3 = 0;  
            for(it = d[1].begin(); it != d[1].end(); ++it)  
                if(!d[0].count(it->first)) t[c1++] = it->first;  
            if(c1) print('+', c1);  
      
            for(it = d[0].begin(); it != d[0].end(); ++it)  
                if(!d[1].count(it->first)) t[c2++] = it->first;  
            if(c2) print('-', c2);  
      
            for(it = d[1].begin(); it != d[1].end(); ++it)  
                if(d[0].count(it->first) && d[0][it->first] != it->second) t[c3++] = it->first;  
            if(c3) print('*', c3);  
      
            if(!(c1 || c2 || c3)) puts("No changes");  
            puts("");  
        }  
        return 0;  
    }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值