#C14958. Merge Two Dictionaries

    ID: 44664 Type: Default 1000ms 256MiB

Merge Two Dictionaries

Merge Two Dictionaries

You are given two dictionaries d1 and d2 in JSON format. Your task is to merge these dictionaries using the following rule:

  • If a key exists in only one dictionary, include it with its corresponding value.
  • If a key exists in both dictionaries, combine their values into a list. In particular, if a value is not already a list, treat it as a singleton list. Then, concatenate the lists. Formally, for any key k present in both dictionaries, the output should satisfy $$\text{output}[k] = \begin{cases} d1[k] & \text{if } k \notin d2, \\ d2[k] & \text{if } k \notin d1, \\ \text{List}(d1[k]) \oplus \text{List}(d2[k]) & \text{if } k \in d1 \text{ and } k \in d2, \end{cases}$$ where \(\text{List}(x)\) denotes x if x is already a list, and [x] otherwise, and \(\oplus\) denotes the list concatenation operation.

The merged dictionary must be output in JSON format.

inputFormat

The input consists of two lines read from standard input:

  1. The first line is a JSON object representing the first dictionary d1.
  2. The second line is a JSON object representing the second dictionary d2.

Each dictionary contains key-value pairs. Values can be integers, strings, or lists (of integers and/or strings).

outputFormat

Output a single JSON object that represents the merged dictionary, following the merging rules described above. The output should be printed to standard output.

## sample
{"a": 1, "b": "two"}
{"c": 3, "d": "four"}
{"a":1,"b":"two","c":3,"d":"four"}