#K48447. Longest Subsequence Word

    ID: 28422 Type: Default 1000ms 256MiB

Longest Subsequence Word

Longest Subsequence Word

You are given a source string \( s \) and a list of words. Your task is to find the length of the longest word in the list that is a subsequence of \( s \). A subsequence is a sequence that can be derived from the string by deleting some or no characters without changing the order of the remaining characters. For example, "apple" is a subsequence of "abpcplea" because you can delete some letters to get "apple".

If no word in the list is a subsequence of \( s \), output 0.

Note: A string \( word \) is a subsequence of \( s \) if there exists a sequence of indices \( 1 \leq i_1 < i_2 < \cdots < i_{|word|} \leq |s| \) such that \( s[i_j] = word[j] \) for all \( j \) from 1 to \( |word| \).

inputFormat

The first line of input contains an integer \( T \), the number of test cases.

Each test case consists of three parts:

  1. A line containing the string \( s \).
  2. A line containing an integer \( n \), representing the number of words.
  3. A line containing \( n \) space-separated words.

It is guaranteed that \( s \) and each word consist of lowercase letters only.

outputFormat

For each test case, output a single line containing one integer – the length of the longest word in the list that is a subsequence of the string \( s \). If no word is a subsequence, output 0.

## sample
2
abpcplea
3
ale apple monkey
abcd
2
ab abc
5

3

</p>