更新字典

In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, andvalues are non-negative integers. Given an old dictionary and a new dictionary, find out what werechanged.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 leadingzeros or prefix ‘+’. (i.e. -4, 03 and +77 are illegal). Each key will appear at most once, but keys canappear in any order.InputThe first line contains the number of test cases T (T ≤ 1000). Each test case contains two lines. Thefirst line contains the old dictionary, and the second line contains the new dictionary. Each line willcontain at most 100 characters and will not contain any whitespace characters. Both dictionaries couldbe empty.WARNING: there are no restrictions on the lengths of each key and value in the dictionary. Thatmeans keys could be really long and values could be really large.OutputFor 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

题意:给出一个旧的字典和一个新的字典,字典里面的单词有两个属性一个是单词本身,一个是单词的权值,

如果新的字典里面有一个单词,而旧的字典里面没有的话,就是属于+(加号)一组的;如果新的字典里面没有单词,而旧的字典里面有的话,就是属于-(减号)一组的;

如果新的字典和旧的字典完全一样的话就输出No changes;如果新的字典和旧的字典单词相同,但是他们的权值不同的话就属于*(星号)一组的

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
    int n,ok;
    char str1[110],str2[110];
    map<string,string> map1,map2,map3,map4;
    string s1,s2;
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        //ok=0;
        map1.clear();
        map2.clear();
        map3.clear();
        map4.clear();
        s1=s2="";
        gets(str1);
        gets(str2);
        int len1=strlen(str1);
        int len2=strlen(str2);
        for(int i=0; i<len1; i++)
        {
            if(str1[i]>='a'&&str1[i]<='z')
                s1=s1+str1[i];
            else if(str1[i]>='0'&&str1[i]<='9')
                s2=s2+str1[i];
            else if(str1[i]==','||i==len1-1)
            {
                if(s1!="")
                map1[s1]=s2;
                s1=s2="";
            }
        }
        for(int i=0; i<len2; i++)
        {
            if(str2[i]>='a'&&str2[i]<='z')
                s1=s1+str2[i];
            else if(str2[i]>='0'&&str2[i]<='9')
                s2=s2+str2[i];
            else if(str2[i]==','||i==len2-1)
            {
                map<string,string>::iterator it;
                it=map1.find(s1);
                if(it==map1.end()&&s1!="")
                {
                    //ok=1;
                    map2[s1]=s2;
                    //cout<<s1<<" "<<s2<<endl;
                    s1=s2="";
                }
                else
                {
                    if(s1=="")
                        continue;
                    map3[s1]=s2;
                    if(it->second!=s2)
                    {
                        //ok=1;
                        map4[s1]=s2;
                    }
                    s1=s2="";
                }
            }
        }
        if(map2.size()==0&&map4.size()==0&&map1.size()==map3.size())
        {
            cout<<"No changes"<<endl;
        }
        else
        {
            int k=0;
            map<string,string>::iterator it;
            if(map2.size()!=0)
            {
                for(it=map2.begin(); it!=map2.end(); it++)
                {
                    if(!k)
                    {
                        cout<<'+'<<it->first;
                        k=1;
                    }
                    else
                        cout<<','<<it->first;

                }
                cout<<endl;
            }
            k=0;
            if(map1.size()>map3.size())
            {
                for(it=map1.begin(); it!=map1.end(); it++)
                {
                    if(map3.find(it->first)==map3.end())
                    {
                        if(!k)
                        {
                            cout<<'-'<<it->first;
                            k=1;
                        }
                        else
                            cout<<','<<it->first;
                    }

                }
                cout<<endl;
            }
            k=0;
            if(map4.size()!=0)
            {
                for(it=map4.begin(); it!=map4.end(); it++)
                {
                    if(!k)
                    {
                        cout<<'*'<<it->first;
                        k=1;
                    }
                    else
                        cout<<','<<it->first;
                }
                cout<<endl;
            }
        }
        cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值