[ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]

简介:


 

  Updating a Dictionary 

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$ \le$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

题目大意:每次给2个字符串,字符串里的内容表示为key:value对,顺序随意,比较2个字符串里的内容判断增加了那些,减少了那些,key值对应的value变了的有哪些。水题,字符串处理,烦!

复制代码
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<string>
  4 #include<string.h>
  5 #include<cstring>
  6 #include<sstream>
  7 #include<algorithm>
  8 using namespace std;
  9 struct A
 10 {
 11     string key;
 12     string value;
 13     int same(A &b){
 14         if(key==b.key){
 15             if(value==b.value)return 0;//no change
 16             else return 1;//change
 17         }
 18         else{//not same
 19             if(key<b.key)return 2;
 20             else return 3;
 21         }
 22     }
 23     void set(string a,string b){
 24         key=a;
 25         value=b;
 26     }
 27 };
 28 bool cmp(A a,A b){
 29     return a.key<b.key;
 30 }//比较函数一定不要用&同名引用
 31 void xiu(string &A){
 32     A=A.substr(1,A.length()-2);
 33     for(int i=A.length()-1;i>=0;i--){
 34         if(A[i]==',' || A[i]==':')A[i]=' ';
 35     }
 36 }
 37 int main(){
 38     int T;cin>>T;
 39     string str1;
 40     string str2;
 41     string value,key;
 42     getline(cin,str1);
 43     while(T--){
 44         getline(cin,str1);
 45         getline(cin,str2);
 46         xiu(str1);
 47         xiu(str2);
 48         istringstream in1(str1);
 49         istringstream in2(str2);
 50         A x1[101],x2[101];
 51         int i=0;
 52         while(in1>>key>>value){
 53             x1[i++].set(key,value);
 54         }
 55         int j=0;
 56         while(in2>>key>>value){
 57             x2[j++].set(key,value);
 58         }
 59         sort(x1,x1+i,cmp);
 60         sort(x2,x2+j,cmp);
 61         string add[101];int add_num=0;
 62         string sub[101];int sub_num=0;
 63         string cha[101];int cha_num=0;
 64         int ii=0,jj=0;
 65         while(ii<i && jj<j){
 66             switch(x1[ii].same(x2[jj])){
 67             case 0:ii++,jj++;break;
 68             case 1:cha[cha_num++]=x1[ii].key;ii++,jj++;break;
 69             case 2:sub[sub_num++]=x1[ii].key;ii++;break;
 70             case 3:add[add_num++]=x2[jj].key;jj++;break;
 71             default:break;
 72             }
 73         }
 74         while(ii<i){
 75             sub[sub_num++]=x1[ii++].key;
 76         }
 77         while(jj<j){
 78             add[add_num++]=x2[jj++].key;
 79         }
 80         if(add_num+sub_num+cha_num==0)cout<<"No changes\n\n";
 81         else{
 82             if(add_num!=0){
 83                 cout<<"+"<<add[0];
 84                 for(int k=1;k<add_num;k++){
 85                     cout<<','<<add[k];
 86                 }
 87                 cout<<'\n';
 88             }
 89             if(sub_num!=0){
 90                 cout<<"-"<<sub[0];
 91                 for(int k=1;k<sub_num;k++){
 92                     cout<<','<<sub[k];
 93                 }
 94                 cout<<'\n';
 95             }
 96             if(cha_num!=0){
 97                 cout<<'*'<<cha[0];
 98                 for(int k=1;k<cha_num;k++){
 99                     cout<<','<<cha[k];
100                 }
101                 cout<<'\n';
102             }
103             cout<<'\n';
104         }
105     }return 0;
106 }
复制代码
相关文章
|
7月前
|
Go 索引
Go 1.22 slices 库的更新:高效拼接、零化处理和越界插入优化
本文详细介绍了 Go 1.22 版本中 slices 库的更新内容,总结起来有三个方面:新增了 Concat 函数、对部分函数新增了零化处理的逻辑和对 Insert 函数进行了越界插入优化
178 1
Go 1.22 slices 库的更新:高效拼接、零化处理和越界插入优化
|
4月前
【Azure Developer】使用PowerShell Where-Object方法过滤多维ArrayList时候,遇见的诡异问题 -- 当查找结果只有一个对象时,返回结果修改了对象结构,把多维变为一维
【Azure Developer】使用PowerShell Where-Object方法过滤多维ArrayList时候,遇见的诡异问题 -- 当查找结果只有一个对象时,返回结果修改了对象结构,把多维变为一维
|
7月前
|
存储 Python
|
人工智能 测试技术
cf1653c通过操作让数组序列呈现某种规律 C. Differential Sorting
cf1653c通过操作让数组序列呈现某种规律 C. Differential Sorting
83 0
|
存储 Java
定义不可变对象的策略---粗略翻译自文档
定义不可变对象的策略---粗略翻译自文档
定义不可变对象的策略---粗略翻译自文档
|
Python
Python经典编程习题100例:第100例:列表转化为字典
Python经典编程习题100例:第100例:列表转化为字典
85 0
|
Python
Python经典编程习题100例:第73例:反向输出列表
Python经典编程习题100例:第73例:反向输出列表
69 0
|
NoSQL 数据可视化 安全
一道思考题所引起动态跟踪 ‘学案’
想想看,当我们用 kprobe 为一个内核函数注册了 probe 之后,怎样能看到对应内核函数的第一条指令被替换了呢?
1022 2
|
Python
python中.format()槽的顺序改变(速看,带图解释)
python中.format()槽的顺序改变(速看,带图解释)
251 0
python中.format()槽的顺序改变(速看,带图解释)
|
Python
python 小技巧,给设计好的dict 增加嵌套值
python 小技巧,给设计好的dict 增加嵌套值
python 小技巧,给设计好的dict 增加嵌套值