• UVA


      Updating a Dictionary 

    In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, and values are non-negative integers. Given an old dictionary and a new dictionary, find out what were changed.

    Each dictionary is formatting as follows:

    {key:value,key:value,...,key:value}

    Each key is a string of lower-case letters, and each value is a non-negative integer without leading zeros or prefix `+'. (i.e. -4, 03 and +77 are illegal). Each key will appear at most once, but keys can appear in any order.

    Input 

    The first line contains the number of test cases T ( T$ le$1000). Each test case contains two lines. The first line contains the old dictionary, and the second line contains the new dictionary. Each line will contain at most 100 characters and will not contain any whitespace characters. Both dictionaries could be empty.


    WARNING: there are no restrictions on the lengths of each key and value in the dictionary. That means keys could be really long and values could be really large.

    Output 

    For each test case, print the changes, formatted as follows:

    • First, if there are any new keys, print `+' and then the new keys in increasing order (lexicographically), separated by commas.
    • Second, if there are any removed keys, print `-' and then the removed keys in increasing order (lexicographically), separated by commas.
    • Last, if there are any keys with changed value, print `*' and then these keys in increasing order (lexicographically), separated by commas.

    If the two dictionaries are identical, print `No changes' (without quotes) instead.

    Print a blank line after each test case.

    Sample Input 

    3
    {a:3,b:4,c:10,f:6}
    {a:3,c:5,d:10,ee:4}
    {x:1,xyz:123456789123456789123456789}
    {xyz:123456789123456789123456789,x:1}
    {first:1,second:2,third:3}
    {third:3,second:2}
    

    Sample Output 

    +d,ee
    -b,f
    *c
    
    No changes
    
    -first
    
    
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    #include <map>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    map<string, string> Dic1;
    map<string, string> Dic2;
    map<string, string>::iterator it, it2;
    vector<string> add;
    vector<string> cut;
    vector<string> change;
    
    void cle()
    {
        Dic1.clear();
        Dic2.clear();
        add.clear();
        cut.clear();
        change.clear();
    }
    
    void init_dic()
    {
        int flag = 0;
        string str, t1, t2;
        getline(cin, str);
        for (int i = 1; i < str.size(); i++)
        {
            if (str[i] == ':')
            {
                flag = 1;
                continue;
            }
            else if (str[i] == ',' || str[i] == '}')
            {
                flag = 0;
                if(!t1.empty() || !t2.empty())
                    Dic2.insert(make_pair(t1, t2));
                t1.clear();
                t2.clear();
                continue;
            }
            if (flag == 0)
                t1 += str[i];
            else if (flag == 1)
                t2 += str[i];
        }
    }
    
    void put(vector<string> & x)
    {
        int flag = 0;
        for (auto i : x)
        {
            if (flag++)
                cout << "," << i;
            else
                cout << i;
        }
        cout << endl;
    }
    
    int main()
    {
    //#ifndef ONLINE_JUDGE
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
    //#endif
    
        int kase;
        cin >> kase;
        cin.get();
        while (cle(), kase--)
        {
            init_dic();
            Dic1 = Dic2;
            Dic2.clear();
            init_dic();
    
            if(!Dic2.empty())
                for (it = Dic2.begin(); it != Dic2.end(); it++)
                {
                    it2 = Dic1.find(it->first);
                    if (it2 == Dic1.end())
                        add.push_back(it->first);
                    else if (it2->second != it->second)
                        change.push_back(it->first);
                }
    
            if(!Dic1.empty())
                for (it = Dic1.begin(); it != Dic1.end(); it++)
                {
                    it2 = Dic2.find(it->first);
                    if (it2 == Dic2.end())
                        cut.push_back(it->first);
                }
    
            sort(add.begin(), add.end());
            sort(cut.begin(), cut.end());
            sort(change.begin(), change.end());
    
            if (add.empty() && cut.empty() && change.empty())
                cout << "No changes" << endl;
            else
            {
                if (!add.empty())
                    putchar('+'), put(add);
                if (!cut.empty())
                    putchar('-'), put(cut);
                if (!change.empty())
                    putchar('*'), put(change);
            }
            putchar('
    ');
        }
        return 0;
    }


  • 相关阅读:
    OOP3(继承中的类作用域/构造函数与拷贝控制/继承与容器)
    OOP2(虚函数/抽象基类/访问控制与继承)
    OOP1(定义基类和派生类)
    拷贝控制3(对象移动)
    拷贝控制2(拷贝控制和资源管理/交换操作/动态内存管理)
    拷贝控制1(拷贝、赋值与销毁)
    动态内存2(动态数组)
    python--numpy模块、spicy模块、 matplotlib模块
    Java--23种设计模式之decorator模式
    Android开发---开发文档翻译
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312780.html
Copyright © 2020-2023  润新知