#C12315. Merge Two Dictionaries
Merge Two Dictionaries
Merge Two Dictionaries
You are given two dictionaries. For keys that appear in both dictionaries, sum their respective values. Then, sort the merged dictionary by keys in lexicographical order and output the result as pairs. The summation for duplicate keys can be expressed as (v_{new} = v_1 + v_2). This is a simple problem that tests your ability to combine data structures and sort outputs. Read the input from standard input (stdin) and print the result to standard output (stdout).
inputFormat
The input is provided via stdin. The first line contains an integer (n_1), representing the number of key-value pairs in the first dictionary. The next (n_1) lines each contain a string key and an integer value separated by a space. Following that, there is a line with an integer (n_2) representing the number of pairs in the second dictionary, followed by (n_2) lines in the same format.
outputFormat
Print the merged key-value pairs sorted in lexicographical order by key. Each line should contain a key and its corresponding summed value separated by a single space. If the merged dictionary is empty, output nothing.## sample
3
a 5
b 2
c 3
3
a 7
b 8
d 6
a 12
b 10
c 3
d 6
</p>