Problem C Updating a Dictionary

Problem C     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≤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

题意;字典更新,第一行输入旧字典,第二行输入新字典;询问由旧字典得到新字典经过那些操作。+代表增加,-代表删除,*代表更改,按字典序输出答案。

 

题解:map+set;

 

 

#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<map>
#include<set>
#define ll long long
using namespace std;
map<string,string>m;
set<string>p1;
set<string>p2;
string s,s1,s2;
int n,x;
int main()
{
    cin>>n;
    while(n--)
    {
        m.clear();
        p1.clear();
        p2.clear();
        s1.clear();
        s2.clear();
        cin>>s;
        for(int i=1;s[i];i++)
        {
            if(isalpha(s[i]))
                s1=s1+s[i];
            else if(isdigit(s[i]))
            {
                s2=s2+s[i];
            }
            else if(s[i]==','||s[i]=='}')
            {
                if(s1!=""&&s2!="")
                    m[s1]=s2;
                s1.clear();
                s2.clear();
            }
        }
        s.clear();
        cin>>s;
        for(int i=1;s[i];i++)
        {
            if(isalpha(s[i]))
                s1=s1+s[i];
            else if(isdigit(s[i]))
            {
                s2=s2+s[i];
            }
            else if(s[i]==','||s[i]=='}')
            {
                if(m.count(s1)&&m[s1]==s2)//存在
                {
                    m.erase(s1);
                    s1.clear();
                    s2.clear();
                    continue;
                }
                else if(m.count(s1)&&m[s1]!=s2)//更改
                {
                    p2.insert(s1);
                    m.erase(s1);
                }
                else if(m.count(s1)==0&&s1!="")//增加
                    p1.insert(s1);

                s1.clear();
                s2.clear();
            }
        }
        set<string>::iterator itt;
        if(!p1.empty())//增加
        {
            cout<<'+';
            for(itt=p1.begin();itt!=p1.end();itt++)
            {
                if(itt==p1.begin())
                    cout<<*itt;
                else
                    cout<<','<<*itt;
            }
            cout<<endl;
        }
        if(!m.empty())
        {
            cout<<'-';
            map<string,string>::iterator it;
            for(it=m.begin();it!=m.end();it++)
            {
                if(it==m.begin())
                    cout<<it->first;
                else
                    cout<<','<<it->first;
            }
            cout<<endl;
        }
        if(!p2.empty())
        {
            cout<<'*';
            for(itt=p2.begin();itt!=p2.end();itt++)
            {
                if(itt==p2.begin())
                    cout<<*itt;
                else
                    cout<<','<<*itt;
            }
            cout<<endl;
        }
        if(p1.empty()&&p2.empty()&&m.empty())
            cout<<"No changes"<<endl;
        cout<<endl;
    }
    
    return 0;
}

 

上一篇:Tshare使用


下一篇:确保线程安全下使用Queue的Enqueue和Dequeue