#C9030. Circular String Chain Verification
Circular String Chain Verification
Circular String Chain Verification
You are given a list of strings. Your task is to determine whether the strings, in the given order, form a valid chain. A chain is valid if for every consecutive pair of strings, the last character of the preceding string is the same as the first character of the following string.
For example, consider the list:
abc cde ef g gab
This list forms a valid chain because:
- The last character of "abc" is 'c' and the first character of "cde" is 'c'.
- The last character of "cde" is 'e' and the first character of "efg" is 'e'.
- The last character of "efg" is 'g' and the first character of "gab" is 'g'.
Note that if there is only one string, it is automatically considered valid, and if the list is empty, the answer is NO
.
Your program should read the input from standard input and write the output to standard output.
Additional Note: The strings must be used in the order in which they are provided; reordering is not allowed.
inputFormat
The input is read from standard input and has the following format:
- The first line contains a single integer
n
(0 ≤ n ≤ 10⁵), representing the number of strings. - The following
n
lines each contain one non‐empty string.
outputFormat
Output a single line to standard output: YES
if the list of strings forms a valid chain, or NO
otherwise.
4
abc
cde
ef g
gab
YES
</p>