CSU - 1113 (STL)

题目链接:CSU - 1113

题目:

1113: Updating a Dictionary

Submit Page     Summary     Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 1106     Solved: 281    

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.

扫描二维码关注公众号,回复: 2814554 查看本文章

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的自动排序

将前后两个字符串的每个密码存入两个set中

然后就是输入 和 密码及其价值如何存到map中 的问题 (详细见代码)

AC代码:

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <map>
#include <set>
using namespace std;

int main()
{
    int t;
    map<string,string>p1,p2; //分别存前后的密码和价值
    set<string>m1; //存之前所有的密码名称
    set<string>m2; //存之后所有的密码名称
    char oldd[105],newd[105];
    scanf("%d",&t);
    while(t--)
    {
        getchar();
        p1.clear();
        p2.clear();
        m1.clear();
        m2.clear();
        //输入和将密码和价值存入map中
        scanf("%s",oldd);
        getchar();
        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())
                {
                    p1[a]=b;
                    m1.insert(a);
                    a="";
                    b="";
                }
            }
        }
        
        scanf("%s",newd);
        getchar();
        len=strlen(newd);
        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())
                {
                    p2[a]=b;
                    m2.insert(a);
                    a="";
                    b="";
                }
            }
        }
        //输出增加的密码
        int flag1=0;
        set <string> :: iterator it;
        for(it=m2.begin();it!=m2.end();it++)
        {
            if(!m1.count(*it))
            {
                if(flag1==0){flag1=1;printf("+");cout<<*it;}
                else cout<<','<<*it;
            }
        }
        if(flag1==1)printf("\n");
        //输出删除的密码
        int flag2=0;
        for(it=m1.begin();it!=m1.end();it++)
        {
            if(!m2.count(*it))
            {
                if(flag2==0){flag2=1;printf("-");cout<<*it;}
                else cout<<','<<*it;
            }
        }
        if(flag2==1)printf("\n");
        //输出改变价值的密码
        int flag3=0;
        for(it=m2.begin();it!=m2.end();it++)
        {
            if(!m1.count(*it));
            else
            {
                string s1=p1[*it];
                string s2=p2[*it];
                if(s1!=s2)
                {
                    if(flag3==0){flag3=1;printf("*");cout<<*it;}
                    else cout<<','<<*it;
                }
            }
        }
        if(flag3==1)printf("\n");
        if(flag1==0&&flag2==0&&flag3==0)printf("No changes\n\n");
        else printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/WQN20172674/article/details/81412775