#K33402. Longest Word by Deleting Characters
Longest Word by Deleting Characters
Longest Word by Deleting Characters
Given a string s
and a list of words, your task is to find the longest word in the list that can be formed by deleting some characters of s
without changing the order of the remaining characters. If there are multiple longest words, you should return the one that is lexicographically smallest. If no word in the list can be formed, print an empty string.
Formally, a word w is a subsequence of s
if it can be derived from s
by deleting some (or no) characters without changing the order of the remaining characters. You need to consider all words in the list, and choose the answer as described.
inputFormat
The input is read from standard input and has the following format:
T s_1 n_1 word_1 word_2 ... word_{n_1} ... s_T n_T word_1 word_2 ... word_{n_T}
Here, the first line contains an integer T
representing the number of test cases. Each test case consists of three lines where the first line is the string s
, the second line is an integer n
representing the number of words, and the third line contains n
words separated by spaces.
outputFormat
For each test case, output the longest word that can be formed by deleting some characters of s
from the given list, following the rules described. If there is no such word, output an empty line.
3
abpcplea
4
apple apply ale plea
abc
4
a b c abc
abc
3
d e f
apple
abc
</p>