湖南省第八届大学生程序设计大赛原题 C

Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

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 

题意:给你两本字典,找两本字典的不同。一个模拟题,注意,两本字典都有可能是空字典,这里被坑了,再次看题发现了坑点。
如果第一本字典没有,而第二本字典里面有,输出+然后输出第二本字典里面有而第一本字典没有的。
如果第二本字典没有,而第一本字典里面有,输入-然后输出第一本字典里面有而第二本字典没有的。
如果第一本字典有并且第二本字典有,但是后面的数字不一样,那么输出*然后输出页码不同的字。
如果两本字典完全一样,输出No changes
注意,每组案例输出后要有空行!
分析:就是无脑的模拟,不解释也能看懂的
#include <iostream>
#include <string>
#include <map>
using namespace std;

map<string,string>s1;
map<string,string>s2;
map<string,string>::iterator it;

string aa[105];

void print(char c,int n)
{
    cout<<c<<aa[0];
    for(int i=1;i<n;i++)
    {
        cout<<","<<aa[i];
    }
    cout<<endl;
}

int main()
{
    string str1;
    string str2;
    int T;
    cin>>T;
    while(T--)
    {
        s1.clear();
        s2.clear();
        cin>>str1;
        int n=str1.size();
        cin>>str2;
        int m=str2.size();
        string a,b;
        for(int i=1;i<n;)
        {
            a="";
            while(str1[i]!=':'&&str1[i]!='}')
                a+=str1[i++];
            if(str1[i]=='}')
                break;
            i++;
            b="";
            while(str1[i]!=','&&str1[i]!='}')
                b+=str1[i++];
            s1[a]=b;
            i++;
        }
        for(int i=1;i<m;)
        {
            a="";
            while(str2[i]!=':'&&str2[i]!='}')
                a+=str2[i++];
            if(str2[i]=='}')
                break;
            i++;
            b="";
            while(str2[i]!=','&&str2[i]!='}')
                b+=str2[i++];
            s2[a]=b;
            i++;
        }
        int c1=0,c2=0,c3=0;
        for(it=s2.begin();it!=s2.end();it++)
        {

            if(!s1.count(it->first))
                aa[c1++]=it->first;
        }
        if(c1)
            print('+',c1);
        for(it=s1.begin();it!=s1.end();it++)
        {

            if(!s2.count(it->first))
               aa[c2++]=it->first;
        }
        if(c2)
            print('-',c2);
        for(it=s2.begin();it!=s2.end();it++)
        {
            if(s1.count(it->first)&&s1[it->first]!=it->second)
               aa[c3++]=it->first;
        }
        if(c3)
            print('*',c3);
        if(c1+c2+c3==0)
            cout<<"No changes"<<endl;
        cout<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wabi87547568/p/4690543.html