#C5836. Replace Words in a Sentence
Replace Words in a Sentence
Replace Words in a Sentence
You are given a sentence s
and a dictionary d
mapping certain words to their replacement definitions. Your task is to replace each word in s
that exactly matches a key in d
with its corresponding value.
The replacement process can be formalized as follows:
\[ \text{For each word } w \text{ in } s, \text{ if } w \in \text{dom}(d) \text{ then replace } w \text{ with } d[w]. \]
Note that the input is provided from standard input (stdin) and the output should be printed to standard output (stdout).
inputFormat
The input consists of multiple lines:
- The first line contains the string
s
. - The second line contains an integer
n
which is the number of entries in the dictionary. - The following
n
lines each contain a dictionary entry. Each entry begins with a key (a word without spaces) followed by a space, and then its replacement string (which may contain spaces).
outputFormat
Output the modified string after replacing every word from the dictionary. The output should be printed to stdout.
## sampleI love programming because it is fun and challenging
3
love enjoy greatly
programming the act of writing computer programs
fun enjoyment or amusement
I enjoy greatly the act of writing computer programs because it is enjoyment or amusement and challenging