[Huawei OD test questions] Word Solitaire (java) 100% pass rate [2023 (B volume) 100 points]

word solitaire

topic description

The rules of Word Solitaire are:
for words that can be used in Solitaire, the first letter must be the same as the last letter of the previous word;
when there are multiple words with the same initial letter, take the longest word;
if the lengths are also equal, take The word with the smallest lexicographical order;
words that have already participated in Solitaire cannot be reused;
now a set of word arrays composed of all lowercase letters is given,
and one of the words is designated as the starting word to perform Word Solitaire,
please output the longest word string .
Word strings are concatenated words with no spaces in between.

Number of words 1 < N < 20
Length of a single word 1 ~ 30

enter description

Enter the first line of a non-negative integer
to indicate the index of the starting word in the array k
0 <= k < N
The second line of the input is a non-negative integer N
and the next N lines represent the words in the word array

output description

Output a string representing the final concatenated word string

example one

enter

0
6
word
dd
da
dc
dword
d

output

worddwordda

Guess you like

Origin blog.csdn.net/weixin_45541762/article/details/132460499