#K59807. Word Replacement with Exact Boundaries

    ID: 30946 Type: Default 1000ms 256MiB

Word Replacement with Exact Boundaries

Word Replacement with Exact Boundaries

In this problem, you are given a text and a sequence of word replacement operations. Each operation consists of a target word and its replacement. You need to replace each occurrence of the target word in the text with the replacement, but only when the target word appears as a whole word. This means that for a valid replacement, the target word must be delimited by either the beginning/end of the text or a non-alphanumeric character.

Formally, if T is the target word, you must replace occurrences that match the regular expression pattern $$\b;\text{T};\b$$ (in LaTeX format, note that the escape for (\backslash b) is required).

The operations are applied sequentially in the order provided. After processing all operations, output the final modified text.

inputFormat

The input is read from standard input (stdin) and has the following format:

  1. The first line contains an integer (n) representing the number of replacement operations.
  2. The second line contains the original text.
  3. The next (n) lines each contain two strings separated by a space: the target word and its replacement.

    Note: The text and the words will not contain extra spaces, and all operations should consider only whole word matches.

outputFormat

Output the modified text after performing all the replacement operations. The answer should be printed to standard output (stdout).## sample

1
hello world
world planet
hello planet

</p>