Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
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 ( T![$ \le$](http://uva.onlinejudge.org/external/125/12504img1.png)
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<bits/stdc++.h> using namespace std; map<string,string> before,after; vector<string> bef,aft; void SCANF(vector<string>& s,map<string,string>& Map) { string s1="",s2=""; char t; while((t=getchar())!='}') { if(t!=':'&&t!=','&&t!='{'&&t!='\n'&&t!=' ') s1+=t; else if(t==':') { s.push_back(s1); while((t=getchar())!=','&&t!='}'&&t!='\n'&&t!=' ') s2+=t; Map[s1]=s2; if(t=='}') break; if(t==',') s1.clear(),s2.clear(); } } } int CHECK1() { int sign=0; for(int i=0;i<aft.size();i++) { if(!before.count(aft[i])) { if(!sign) { printf("+"),sign=1; cout<<aft[i]; } else cout<<","<<aft[i]; } } if(sign) printf("\n"); return sign; } int CHECK2() { int sign=0; for(int i=0;i<bef.size();i++) { if(!after.count(bef[i])) { if(!sign) { printf("-"),sign=1; cout<<bef[i]; } else cout<<","<<bef[i]; } } if(sign) printf("\n"); return sign; } int CHECK3() { int sign=0; for(int i=0;i<bef.size();i++) { if(after.count(bef[i])) { if(after[bef[i]]!=before[bef[i]]) { if(!sign) { printf("*"),sign=1; cout<<bef[i]; } else cout<<","<<bef[i]; } } } if(sign) printf("\n"); return sign; } int main() { int n,a1,a2,a3; scanf("%d",&n); getchar(); for(int i=0;i<n;i++) { SCANF(bef,before); SCANF(aft,after); sort(bef.begin(),bef.end()); sort(aft.begin(),aft.end()); // for(int i=0;i<bef.size();i++) // cout<<bef[i]; // printf("\n"); // for(int i=0;i<aft.size();i++) // cout<<aft[i]; a1=CHECK1(); a2=CHECK2(); a3=CHECK3(); if(!a1&&!a2&&!a3) printf("No changes\n"); printf("\n"); bef.clear(); aft.clear(); before.clear(); after.clear(); } return 0; }