UVA - 12504 Updating a Dictionary(map+字符串处理,模拟)

这是STL要写的最后一道题了,终于要到数据结构基础了~
题目链接

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.

一看题就知道需要字符串分割和map记忆,思路:

1.首先如何用map存起来,观察可知’:‘是分开key和value的关键字符,而’,‘是区分两个键值对的关键字符,至于{}两个字符直接跳过就行(刚开始因为只跳过了’{'而没有跳右括号WA了)

2.用三个string数组来存增加的,删除的和变化的,再就是如何发现增加的,删除的,变化的以及不变的,再对字符串s2操作时,每提取出一个键值对,如果在map中没有找到,就是增加的。如果找到了分两种情况,一是值不相等,这时应该就是改变的,如果相等就是不变的。这时需要注意,以上每个操作都需要把map中的值变为空串"",那删除的怎么处理呢?使用迭代器遍历map,如果一个值不为空串,就把它的键存起来,就是删除的

3.上面可能会疑惑为什么存成空串,可以实地操作:如果在一个map中去查找本不存在的键,那么这个键会被自动生成,生成后对应的值是空,即0或者空串或者空字符。

注意本题的三个坑,这也是我WA两次的原因:

  • 输入的两个字符串可能为空的,即{}
  • 需要将每个输出都升序输出(sort即可)
  • 每次输出完需要再来一个换行

代码:

#include <iostream>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
string c1[105],c2[105],c3[105];
map<string,string> mp;
int main()
{
    string s1,s2;
    int n;
    cin>>n;
    while(n--){
        mp.clear();
        cin>>s1>>s2;
        int m1=0,m2=0,m3=0;
        for(int i=0;i<s1.size();i++){
            if(s1[i]=='{'||s1[i]=='}'){ ///易错点一
                continue;
            }else{
                string ss="",sss="";
                while(s1[i]!=':'){
                    ss+=s1[i];
                    i++;
                }
                i++;
                while(s1[i]!=','&&s1[i]!='}'){
                    sss+=s1[i];
                    i++;
                }
                mp[ss]=sss;
            }
        }
        /*for(map<string,string>::iterator i=mp.begin();i!=mp.end();i++)
            cout<<i->first<<"--"<<i->second<<endl;*/
        for(int i=0;i<s2.size();i++){
            if(s2[i]=='{'||s2[i]=='}'){
                continue;
            }else{
                string ss="",sss="";
                while(s2[i]!=':'){
                    ss+=s2[i];
                    i++;
                }
                i++;
                while(s2[i]!=','&&s2[i]!='}'){
                    sss+=s2[i];
                    i++;
                }
                if(mp[ss]==""){
                    c1[++m1]=ss;
                }else if(mp[ss]!=sss){
                    c3[++m3]=ss;
                    mp[ss]="";
                }else mp[ss]="";
            }
        }
        /*for(map<string,string>::iterator i=mp.begin();i!=mp.end();i++)
            cout<<i->first<<"--"<<i->second<<endl;*/
        for(map<string,string>::iterator i=mp.begin();i!=mp.end();i++)
            if(i->second!="")
                c2[++m2]=i->first;
        //cout<<m1<<m2<<m3<<endl;
        if(m1==0&&m2==0&&m3==0){
            cout<<"No changes\n\n";
            continue;
        }
        if(m1!=0){
            sort(c1+1,c1+1+m1); ///易错点二
            cout<<"+"<<c1[1];
            for(int i=2;i<=m1;i++)
                cout<<","<<c1[i];
            cout<<endl;
        }
        if(m2!=0){
            sort(c2+1,c2+1+m2);
            cout<<"-"<<c2[1];
            for(int i=2;i<=m2;i++)
                cout<<","<<c2[i];
            cout<<"\n";
        }
        if(m3!=0){
            sort(c3+1,c3+1+m3);
            cout<<"*"<<c3[1];
            for(int i=2;i<=m3;i++)
                cout<<","<<c3[i];
            cout<<"\n";
        }
        cout<<endl; ///易错点三
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值