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:
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 ( T1000). 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 }