zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    vue 组件开发 props 验证
    vue中$emit与$on
    vue中的 ref 和 $refs
    Animate.css动画特效
    Css Tada动画效果(Css Tada Animation Effect)--- shake抖动效果
    给某个dom对象添加动画fadeIn、fadeInDown、flipInY、jackInTheBox
    uniapp导航栏自定义按钮及点击事件
    uniapp的微信小程序,获取授权,获取中文街道地理位置
    在mac上如何用safari调试ios手机的移动端页面
    条件编译
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312780.html
Copyright © 2011-2022 走看看