#K6861. Concatenated Word Pair
Concatenated Word Pair
Concatenated Word Pair
Given a list of words and a target string, your task is to determine whether it is possible to form the target by concatenating exactly two distinct words from the list. The concatenation can be in any order (i.e. either word can come first), but if both orders are possible, you must return the lexicographically smallest valid pair.
If a valid pair is found, output the two words separated by a single space. If no such pair exists, output NO
.
Formally, given a list \(W = \{w_1, w_2, \dots, w_n\}\) and a target string \(T\), find two distinct words \(a\) and \(b\) from \(W\) such that either:
[ a + b = T \quad \text{or} \quad b + a = T ]
and among all such valid pairs, the pair \((a, b)\) is lexicographically smallest.
inputFormat
The first line of input contains an integer \(n\) representing the number of words. The next \(n\) lines each contain a word. The final line contains the target string \(T\).
You should read input from standard input (stdin).
outputFormat
If a valid pair of words exists such that their concatenation (in either order) equals the target, output the two words separated by a space. Otherwise, output NO
.
Output your answer to standard output (stdout).
## sample6
cat
dog
mouse
catdog
mou
dogcat
catdog
cat dog