Computing for eighth-year college students in HunanProgramDesign Competition question CUpdating a dictionary(Question link ).
Problem C 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 ≤ 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 cocould be empty.
Warning:There are no restrictions on the lengths of each key and value in the dictionary. That means keys cocould be really long and values cocould 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
Solution: the question must be output in Lexicographic Order. Map And Set All follow Operator < Automatic Sorting, exactly String Class has Operator < Therefore, use these two functions. Map Map one Key, Value Yes. Insert the data from the old dictionary into this Map . The new dictionary is compared with it. If it is not found, add it Add Collection. If yes, delete it. Pre-deletion judgment Value Whether the values are the same. If they are different, insert Change Set. In this way, what is not deleted is "-" . Then output successively Add Set, "-" Elements and Change Set elements. C ++ Language Code As follows:
# Include <Cstdio> # Include <Cstdlib> # Include <String> # Include <Cctype> # Include <Map> # Include <Set> Using Namespace STD; # Define Max_length 200 Int Main (){ Int Test_cases; Char Line [max_length]; String Key, value; Map <String , String > Dic; Set < String > Add, change; scanf ( " % D " ,& Test_cases); gets (line ); While (Test_cases -- ) {Key = Value = "" ; DIC. Clear (); gets (line ); For ( Int I = 1 ; Line [I]! = ' \ 0 ' ; I ++ ){ If (Isdigit (line [I]) Value + = Line [I]; Else If (Isalpha (line [I]) Key + = Line [I]; Else If (Line [I] = ' , ' ) {DIC. insert (make_pair < String , String > (Key, value); Key = Value = "" ;} Else If (Line [I] = ' } ' ){ If (Key! = "" ) DIC. insert (make_pair < String , String > (Key, value); Key = Value = "" ;}} Add. Clear (); change. Clear (); gets (line ); For ( Int I = 1 ; Line [I]! = ' \ 0 ' ; I ++ ){ If (Isdigit (line [I]) Value + = Line [I]; Else If (Isalpha (line [I]) Key + = Line [I]; Else If (Line [I] = ' , ' ) {Map < String , String >:: Iterator it = Dic. Find (key ); If (It =Dic. End () Add. insert (key ); Else { If (It-> second! = Value) change. insert (key); DIC. Erase (it);} key = Value = "" ;} Else If (Line [I] = ' } ' ){ If (Key! = "" ) {Map < String , String >:: Iterator it = Dic. Find (key ); If (It = Dic. End () Add. insert (key ); Else { If (It-> second! = Value) change. insert (key); DIC. Erase (it);} key = Value = "" ;}}} If (DIC. Size () = 0 & Add. Size () = 0 & Amp; change. Size () = 0 ) Puts ( " No changes " ); Else { If (Add. Size ()! = 0 ){ Set < String > : Const_iterator it; it = Add. Begin (); printf ( " + % S " , It->C_str ()); For (++ It; it! = Add. End (); ++ It) printf ( " , % S " , It-> C_str (); puts ( "" );} If (DIC. Size ()! = 0 ) {Map < String ,String > : Const_iterator it; it = Dic. Begin (); printf ( " -% S " , It-> First. c_str ()); For (++ It; it! = DIC. End (); ++ It) printf ( " , % S " , It-> First. c_str (); puts ( "" );} If (Change. Size ()! = 0 ){ Set < String > : Const_iterator it; it = Change. Begin (); printf ( " * % S " , It-> C_str ()); For (++ It; it! = Change. End (); ++ It) printf ( " , % S " , It-> C_str (); puts ( "" ) ;}} Puts ( "" );} Return Exit_success ;}