#C11549. Concatenated Pair Finder

    ID: 40877 Type: Default 1000ms 256MiB

Concatenated Pair Finder

Concatenated Pair Finder

Given a list of words and a target string, determine whether the target can be formed by concatenating exactly two words from the list. If such a pair exists, output the two words separated by a space. It is guaranteed that if there exists a valid pair, at least one of them will occur in the list with sufficient frequency (if both words are the same, the list must contain that word at least twice). If no such pair exists, output an empty line.

Formally, given a list \(W = [w_1, w_2, \ldots, w_n]\) and a target string \(T\), find two words \(a\) and \(b\) in \(W\) (not necessarily distinct, but if they are the same then \(w_i\) must appear at least twice) such that \[ T = a + b \] where \(a + b\) denotes the concatenation of \(a\) and \(b\). If multiple valid pairs exist, output any one of them.

inputFormat

The input is given via standard input (stdin) and consists of three lines:

  1. The first line contains an integer (n) representing the number of words in the list.
  2. The second line contains (n) words separated by spaces.
  3. The third line contains the target string (T).

Note: If (n = 0), the second line will be empty.

outputFormat

Print the valid pair of words separated by a space if such a pair exists. If no such pair can be found, output an empty line.## sample

6
cat dog cats sand and catdog
catdog
cat dog