#C13850. Swap Dictionary Keys and Values

    ID: 43434 Type: Default 1000ms 256MiB

Swap Dictionary Keys and Values

Swap Dictionary Keys and Values

You are given a dictionary in a specific input format. Your task is to swap its keys and values so that the original values become the keys and the original keys become the values. In case of duplicate values (which become duplicate keys after swapping), the last key-value pair in the input should override any previous ones.

Note: The input is provided via stdin and the output must be written to stdout.

The input format is described below.

Example:

Input:
3
 a 1
 b 2
 c 3

Output: 3 1 a 2 b 3 c

</p>

inputFormat

The first line contains a single integer N, representing the number of key-value pairs in the dictionary. Each of the following N lines contains two tokens separated by a space. The first token represents the key and the second token represents the value.

For example, the input for the dictionary {'a': 1, 'b': 2, 'c': 3} will be:

3
 a 1
 b 2
 c 3

outputFormat

First, output an integer M that is the number of swapped key-value pairs. Then output M lines, each containing two tokens separated by a space. Each line represents a key-value pair in the swapped dictionary. The key is the original value and the value is the original key. In case of duplicate values in the original dictionary, only the pair corresponding to the last occurrence should be retained and its position in the output should reflect the order of its appearance.

For example, if the input is:

2
 a 1
 b 1

Then the output should be:

1
 1 b
## sample
3
a 1
b 2
c 3
3

1 a 2 b 3 c

</p>