• 湖南省第八届大学生计算机程序设计竞赛1113: Updating a Dictionary (map容器)


     

    1113: Updating a Dictionary
    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 544  Solved: 134
    [Submit][Status][Web Board]
    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
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include<map>
    using namespace std;
    map<string,string> ma;
    string str1,str2;
    void strin(string str)
    {
        int a=0;
        while(str.find(',',a+1)<str.size())   //str.find()返回','所在串中的位置,没找到就返回str.size()+1=str.end();
        {
            string s(str,a+1,str.find(',',a+1)-(a+1));//str.find(',',a+1)-(a+1)表示a+1位置到','之间的长度
            string x(s,0,s.find(':',0));
            //string y(s,s.find(':',0)+1,s.end()-s.find(':',0)-1)//s.end()返回一个迭代器,指向字符串的末尾。即最后一个字符的下一个位置
            string y(s,s.find(':',0)+1,s.size()-s.find(':',0));  //s.size()返回字符串中字符的数量
            ma.insert(pair<string,string>(x,y));
            a=str.find(',',a+1);
        }
        string s(str,a+1,str.find('}',a+1)-a-1);
        string x(s,0,s.find(':',0));
        string y(s,s.find(':',0)+1,s.size()-s.find(':',0));
        if(x.size()!=0)
        ma.insert(pair<string,string>(x,y));
    }
    int main()
    {
        string add[105],red[105],cha[105];
        int t;
        cin>>t;
        while(t--)
        {
            cin>>str1>>str2;
            strin(str1);
            int a=0;
            int i=0,j=0,k=0;
            while(str2.find(',',a+1)<str2.size())
            {
                string s(str2,a+1,str2.find(',',a+1)-(a+1));
                string x(s,0,s.find(':',0));
                string y(s,s.find(':',0)+1,s.size()-s.find(':',0));
                if(ma.find(x)==ma.end())
                    add[i++]=x;
                else
                {
                    if(ma.find(x)->second!=y)
                        cha[k++]=x;
                    ma.erase(x);
                }
                a=str2.find(',',a+1);
            }
            string s1(str2,a+1,str2.find('}',a+1)-(a+1));
            string x1(s1,0,s1.find(':',0));
            string y1(s1,s1.find(':',0)+1,s1.size()-s1.find(':',0));
            if(x1.size()!=0)  //【第二个字典是空字典】
            {
                if(ma.find(x1)==ma.end())
                    add[i++]=x1;
                else
                {
                    if(ma.find(x1)->second!=y1)
                        cha[k++]=x1;
                    ma.erase(x1);
                }
            }
            while(ma.begin()!=ma.end())
            {
                red[j++]=ma.begin()->first;
                ma.erase(ma.begin()->first);
            }
            if(i==0&&j==0&&k==0)
                cout<<"No changes"<<endl;
            else
            {
                if(i!=0)
                {
                    sort(add,add+i);
                    cout<<"+"<<add[0];
                    for(int ii=1; ii<i; ii++)
                        cout<<","<<add[ii];
                    cout<<endl;
                }
                if(j!=0)
                {
                    sort(red,red+j);
                    cout<<"-"<<red[0];
                    for(int jj=1; jj<j; jj++)
                        cout<<","<<red[jj];
                    cout<<endl;
                }
                if(k!=0)
                {
                    sort(cha,cha+k);
                    cout<<"*"<<cha[0];
                    for(int kk=1; kk<k; kk++)
                        cout<<","<<cha[kk];
                    cout<<endl;
                }
            }
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Java-使用IO流对大文件进行分割和分割后的合并
    Java-单向链表算法
    Java-二分查找算法
    Java-二叉树算法
    Java-对象比较器
    Android中Activity的四种开发模式
    Struts2工作原理
    C++实现单例模式
    数组中有一个数字出现的次数超过数组的一半,请找出这个数字
    c++ enum用法【转】
  • 原文地址:https://www.cnblogs.com/dshn/p/4750599.html
Copyright © 2020-2023  润新知