#C13518. Merge Two Dictionaries
Merge Two Dictionaries
Merge Two Dictionaries
You are given two dictionaries (also known as maps) that have string keys and integer values. The task is to merge these two dictionaries into a single one. If a key appears in both dictionaries, then the resulting dictionary should contain that key with its value being the sum of the values from the two dictionaries. Otherwise, include the key with its value from whichever dictionary contains it.
In mathematical terms, for a key k, the merged dictionary's value is given by:
[ \text{merged}(k)=\begin{cases} d_1(k)+d_2(k) & \text{if } k \text{ exists in both } d_1 \text{ and } d_2,\ d_1(k) & \text{if } k \text{ exists only in } d_1,\ d_2(k) & \text{if } k \text{ exists only in } d_2. \end{cases} ]
Your program should read the input from stdin and output the merged dictionary in JSON format to stdout.
inputFormat
The input consists of two lines. Each line is a valid JSON object representing a dictionary. The keys are strings enclosed in double quotes and the values are integers. For example:
{"a": 1, "b": 2} {"b": 3, "c": 4}
outputFormat
The output should be a single JSON object representing the merged dictionary. The keys should be printed in double quotes. For the sample input above, the expected output is:
{"a":1,"b":5,"c":4}## sample
{"a": 1, "b": 2}
{"c": 3, "d": 4}
{"a":1,"b":2,"c":3,"d":4}