CSU 1113 Updating a Dictionary (湖南省第八届大学生计算机程序设计竞赛)

本文解析了一道关于更新字典的数据结构算法题,重点介绍了如何通过使用map数据结构来对比两个字典的差异,包括新增、删除及值变化的情况,并提供了完整的C++代码实现。

1113:Updating a Dictionary

    Time Limit: 1 Sec    Memory Limit: 128 Mb    Submitted: 920    Solved: 224   

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的题目,给你两个串,里面包括对应的键和值,第二个和第一个比较,第二个里面有的而第一个里面没有的说明是增加的,第二个里面没有的而第一个里面有的说明是减少的,两个里面都有的而值不一样,说明值改变了,输出这种情况就可以了,如果都没有出现,那么输出No changes。。。

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int main()
{
    int t;
    string s1,s2,s3,s4;
    scanf("%d",&t);
    while(t--)
    {
        map<string,string>M1;
        map<string,int>M2;///标记
        int a[10001]={0};
        cin>>s1;
        cin>>s2;
        int l1=s1.length(),l2=s2.length(),j=1;
        for(int i=1;i<l1-1;i++)///寻找第一个串里面出现的符号的位置
            if(s1[i]==':' || s1[i]==',') a[j++]=i;
        a[j]=l1-1;
        for(int g=1;g<=j;g++)///利用符号的位置将相对应的值找出来,用map存起来
        {
            int k=0;
            for(int i=a[g-1]+1;i<a[g];i++)
                if(g&1) s3+=s1[i];
                else s4+=s1[i];
            if(g%2==0) {M1[s3]=s4; s3="";s4="";}
        }
        j=1;
        for(int i=1;i<l2-1;i++)
            if(s2[i]==':' || s2[i]==',') a[j++]=i;
        a[j]=l2-1;
        for(int g=1;g<=j;g++)
        {
            int k=0;
            for(int i=a[g-1]+1;i<a[g];i++)
                if(g&1) s3+=s2[i];
                else s4+=s2[i];
            if(g%2==0) {
                if(M1[s3]=="") M2[s3]=1;///增加的
                else if(M1[s3]!=s4) M2[s3]=2;///值改变的
                else M2[s3]=10;
                s3="";s4="";
            }
        }
        int f=1,ff=1;
        map<string,int>::iterator it;
        for(it=M2.begin();it!=M2.end();it++)///增加的键
        {
            if(it->second == 1){
                if(f) {cout<<"+"<<it->first; f=ff=0;}
                else cout<<","<<it->first;
            }
        }
        if(!f)cout<<endl;
        f=1;
        map<string,string>::iterator itt;
        for(itt=M1.begin();itt!=M1.end();itt++)///减少的键
        {
            if(M2[itt->first]!=1 && M2[itt->first]!=10 && M2[itt->first]!=2){
                if(f) {cout<<"-"<<itt->first; f=ff=0;}
                else cout<<","<<itt->first;
            }
        }
        if(!f)cout<<endl;
        f=1;
        for(it=M2.begin();it!=M2.end();it++)///值改变的键
        {
            if(it->second == 2){
                if(f) {cout<<"*"<<it->first; f=ff=0;}
                else cout<<","<<it->first;
            }
        }
        if(!f) cout<<endl;
        if(ff) cout<<"No changes"<<endl;
        if(t) cout<<endl;///最后一个样例不需要输出换行
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值