CSU1113——Updating a Dictionary(STL)

来源:互联网 发布:消防工程师教材淘宝 编辑:程序博客网 时间:2024/06/17 23:42

Description
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搞一搞就行了

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <math.h>#include <algorithm>#include <queue>#include <iomanip>#include <map>#define INF 0x3f3f3f3f#define MAXN 1005#define Mod 99999999using namespace std;char newd[200],oldd[200];string jian[200],jia[200],bian[200];map<string,string> table1,table2;int main(){    int t;    scanf("%d",&t);    while(t--)    {        table1.clear();        table2.clear();        scanf("%s",oldd);        int len=strlen(oldd);        string a="",b="";        for(int i=0; i<len; ++i)        {            if(oldd[i]>='a'&&oldd[i]<='z')                a+=oldd[i];            else if(oldd[i]>='0'&&oldd[i]<='9')                b+=oldd[i];            if(oldd[i]==','||oldd[i]=='}')            {                if(!a.empty()||!b.empty())                {                    table1[a]=b;                    a="";                    b="";                }            }        }        scanf("%s",newd);        len=strlen(newd);        a="",b="";        for(int i=0; i<len; ++i)        {            if(newd[i]>='a'&&newd[i]<='z')                a+=newd[i];            else if(newd[i]>='0'&&newd[i]<='9')                b+=newd[i];            if(newd[i]==','||newd[i]=='}')            {                if(!a.empty()||!b.empty())                {                    table2[a]=b;                    a="";                    b="";                }            }        }        int jianp=0,jiap=0,bianp=0;        map<string,string>::iterator it;        for(it=table1.begin(); it!=table1.end(); ++it)        {            if(table2.find(it->first)==table2.end())                jian[jianp++]=it->first;            else if(it->second!=table2[it->first])                bian[bianp++]=it->first;        }        for(it=table2.begin(); it!=table2.end(); ++it)        {            if(table1.find(it->first)==table1.end())                jia[jiap++]=it->first;        }        //cout<<jiap<<jianp<<bianp<<endl;        if(jiap==jianp&&jianp==bianp&&bianp==0)        {            cout<<"No changes"<<endl;            cout<<endl;        }        else        {            if(jiap!=0)            {                cout<<"+"<<jia[0];                for(int i=1; i<jiap; ++i)                    cout<<","<<jia[i];                cout<<endl;            }            if(jianp!=0)            {                cout<<"-"<<jian[0];                for(int i=1; i<jianp; ++i)                    cout<<","<<jian[i];                cout<<endl;            }            if(bianp!=0)            {                cout<<"*"<<bian[0];                for(int i=1; i<bianp; ++i)                    cout<<","<<bian[i];                cout<<endl;            }            cout<<endl;        }    }    return 0;}
0 0
原创粉丝点击