#C2000. Replace Words with Synonyms
Replace Words with Synonyms
Replace Words with Synonyms
You are given a list of words and a dictionary that maps some words to their synonyms. Your task is to replace each word in the list with its corresponding synonym if one exists in the dictionary. Otherwise, the word should remain unchanged.
Note: The input will first provide the number of words, followed by the list of words separated by spaces. Then, you will be given the number of synonym pairs, followed by each pair on a new line.
Example:
Input: 4 happy sad angry joyful 3 happy cheerful sad unhappy angry mad</p>Output: cheerful unhappy mad joyful
Implement the solution so that it reads from standard input and outputs the result to standard output.
inputFormat
The input is given in the following format:
- The first line contains an integer N — the number of words.
- The second line contains N words separated by spaces.
- The third line contains an integer M — the number of synonym pairs.
- The following M lines each contain a pair of strings: a word and its synonym, separated by a space.
If N is 0, then the list of words is empty.
outputFormat
Output the transformed list of words in one line, separated by a single space. Each word should be replaced by its synonym if it exists; otherwise, it remains unchanged.
## sample4
happy sad angry joyful
3
happy cheerful
sad unhappy
angry mad
cheerful unhappy mad joyful