#P1341. Reconstructing a Letter Chain
Reconstructing a Letter Chain
Reconstructing a Letter Chain
You are given n distinct unordered pairs of letters (the letters are case‐sensitive, and unordered means that for a pair \(\{a, b\}\), the order of \(a\) and \(b\) does not matter). Construct a string of length \(n+1\) such that every given letter pair appears as two consecutive letters in the string (in either order).
Formally, let the input pairs be \(\{x_i, y_i\}\) for \(i=1,2,\dots,n\). You need to find a string \(S = s_1s_2\cdots s_{n+1}\) where each \(s_j\) is a letter, and for each \(i\) there exists an index \(j\) (\(1 \le j \le n\)) such that \(\{s_j, s_{j+1}\} = \{x_i, y_i\}\).
Note: It is guaranteed that the given pairs can form a valid chain (in other words, they form a simple path when interpreted as edges in an undirected graph).
inputFormat
The first line contains an integer \(n\) (\(1 \le n \le 100\)), the number of unordered letter pairs.
Each of the following \(n\) lines contains a pair of letters (each letter is an English alphabet character, case-sensitive) without any separator or with a space between them.
outputFormat
Output a string of length \(n+1\) that contains every given letter pair as a pair of consecutive letters (order within the pair does not matter).
sample
3
ab
bc
cd
abcd