#K8951. Longest Subsequence of Strings
Longest Subsequence of Strings
Longest Subsequence of Strings
You are given a list of strings. Your task is to determine the length of the longest subsequence of these strings such that each string in the subsequence is a subsequence of the string that follows it.
A string s is a subsequence of a string t if there exist indices $i_1, i_2, \dots, i_k$ (with $1 \le i_1 < i_2 < \dots < i_k \le |t|$) satisfying:
$$ s = t[i_1]t[i_2]\dots t[i_k] $$The goal is to select a subsequence of the given strings (not necessarily contiguous in the original list) that satisfies the above property and has maximum possible length.
Note: The order of strings can be rearranged to achieve the longest sequence.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer n representing the number of strings.
- The next n lines each contain a single string.
outputFormat
Output a single integer on standard output (stdout) — the length of the longest subsequence where each string is a subsequence of the next string.
## sample5
a
abc
ab
abcd
b
4