k-更新字典

来源:互联网 发布:sem和seo利与弊端 编辑:程序博客网 时间:2024/05/29 11:40

subject  

       In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, andvalues are non-negative integers. Given an old dictionary and a new dictionary, find out what werechanged.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 leadingzeros or prefix ‘+’. (i.e. -4, 03 and +77 are illegal). Each key will appear at most once, but keys canappear in any order.

Input

          The first line contains the number of test cases T (T ≤ 1000). Each test case contains two lines. Thefirst line contains the old dictionary, and the second line contains the new dictionary. Each line willcontain at most 100 characters and will not contain any whitespace characters. Both dictionaries couldbe empty.WARNING: there are no restrictions on the lengths of each key and value in the dictionary. Thatmeans 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


    坑点:输出按照字典序排序,空格不能做记录。


   我的思路就是分别存起来,不同的都存齐来,在按照平常的思想,看谁有,或者不同的,存入对应的string数组中去,sort排序一下输出就可以了。

就是在存入的时候,估计代码过长,不好看,不过思路是非常清晰的,如果有大神的话,我的输入部分你就可以去改一下,然后,代码长度就会非常短。不过我现在的代码适合新手们看看,和让大佬们知道我们的想法,毕竟我们学的就是解题的想法和思路,不是代码优化,因为那只是在学会的基础上面,进一步的学习。


我的代码如下:

   

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<map>using namespace std;///思路,///分开标记,一个标记一个///不同的存起来。map<string,string> p;map<string,string> xia;string temp[205];string jian[105],jia[105],chen[105];///还有一个坑就是,空格的名字,不能要,应该去除.int main(){    int n;    cin>>n;    getchar();    string a,b;    char str[105];    for(int i=0;i<n;i++)    {       ///清除之前的标记。        p.clear();        xia.clear();        gets(str);        int q=0;        int k=0;        for(int j=1;j<strlen(str);j++)        {            if(str[j]=='}')            {                p[a]=b;                if(a[0]!=' ')                temp[k++]=a;                a="";                b="";                break;            }            if(str[j]==':'){                q=1;                continue;            }            if(str[j]==','){                p[a]=b;                if(a[0]!=' ')                temp[k++]=a;                a="";                b="";                q=0;                continue;            }            if(q==0){                a+=str[j];            }else{                b+=str[j];            }        }        gets(str);         q=0;        for(int j=1;j<strlen(str);j++)        {            if(str[j]=='}')            {                xia[a]=b;                if(p[a]=="" && a[0]!=' ')                  temp[k++]=a;                a="";                b="";                break;            }            if(str[j]==':'){                q=1;                continue;            }            if(str[j]==','){                xia[a]=b;                if(p[a]=="" && a[0]!=' ')                  temp[k++]=a;                a="";                b="";                q=0;                continue;            }            if(q==0){                a+=str[j];            }else{                b+=str[j];            }        }        int flag=0;        int ji = 0;        int ta = 0;        int ch = 0;        for(int j=0;j<k;j++)        {            ///上下相同的            if(p[temp[j]] == xia[temp[j]]) {                flag ++;                continue;            }            if(p[temp[j]]!="" && xia[temp[j]]!="")            {                chen[ch++]=temp[j];                continue;            }            if(p[temp[j]]!="" && xia[temp[j]]==""){                jian[ta++]=temp[j];                continue;            }            if(p[temp[j]]=="" && xia[temp[j]]!=""){                jia[ji++]=temp[j];                continue;            }        }        ///按照字典序排序        if(flag==k){            printf("No changes\n");        }else{            if(ji!=0){               sort(jia,jia+ji);                printf("+");                cout<<jia[0];                for(int j=1;j<ji;j++){                    cout<<","<<jia[j];                }                cout<<endl;            }            if(ta!=0){                    sort(jian,jian+ta);                printf("-");                cout<<jian[0];                for(int j=1;j<ta;j++){                    cout<<","<<jian[j];                }                cout<<endl;            }            if(ch!=0){                    sort(chen,chen+ch);                printf("*");                cout<<chen[0];                for(int j=1;j<ch;j++){                    cout<<","<<chen[j];                }                cout<<endl;            }        }        cout<<endl;    }    return 0;}


给一个大神的代码吧,你们看懂了,在看看简洁的一些的代码,更好一点。

#include<cstdio>#include<cstring>#include<iostream>#include<sstream>#include<map>#include<vector>#include<algorithm>using namespace std;typedef map<string, string> DICT;DICT dict1, dict2;void input_dict(DICT& dict){    dict.clear();    string s, key, value;    getline(cin, s);    for (int i = 0; i < s.size(); i++) {        if (!isdigit(s[i]) && !isalpha(s[i]))          s[i] = ' ';    }    stringstream ss(s);    while (ss >> key >> value)        dict[key] = value;}void print_keys(char ch, vector<string> vs){    printf("%c", ch);    for (int i = 0; i < vs.size(); i++)      cout << vs[i] << ((i == vs.size()-1) ? '\n' : ',');}int main(){    int kase;    cin >> kase;    getchar();    while (kase--) {        input_dict(dict1);        input_dict(dict2);        vector<string> add, del, modi;        DICT::iterator it1, it2;        for (it1 = dict1.begin(); it1 != dict1.end(); it1++) {            it2 = dict2.find(it1->first);            if (it2 == dict2.end()) del.push_back(it1->first);            else if (it2->second != it1->second) modi.push_back(it1->first);        }        for (it2 = dict2.begin(); it2 != dict2.end(); it2++) {            it1 = dict1.find(it2->first);            if (it1 == dict1.end()) add.push_back(it2->first);        }        if (add.size()) print_keys('+', add);        if (del.size()) print_keys('-', del);        if (modi.size()) print_keys('*', modi);        if (!add.size() && !del.size() && !modi.size()) printf("No changes\n");        printf("\n");    }    return 0;}

代码出自于:梁山伯liangrx06。这位大佬。

原创粉丝点击