#P2516. Longest Common Subsequence and Count
Longest Common Subsequence and Count
Longest Common Subsequence and Count
Given two character sequences and , a subsequence of a sequence is formed by deleting zero or more characters (not necessarily contiguous) from the sequence. A sequence is a subsequence of if there exists a strictly increasing index sequence such that for every , we have . For example, if , then is a subsequence of .
The task is to find the length of the longest common subsequence (LCS) of the two given sequences and count the number of distinct longest common subsequences. Two subsequences are considered the same if they result in the same string.
Formally, let be the length of the LCS. You need to compute:
[
L = \max{ k : \exists~{i_0,i_1,\cdots,i_{k-1}}, {j_0,j_1,\cdots,j_{k-1}} \text{ with } x_{i_p} = y_{j_p}~\forall p },
]
and the number of distinct subsequences such that each is common to both sequences and has length .
inputFormat
The input consists of two lines. The first line contains the first character sequence, and the second line contains the second character sequence. Both sequences consist of uppercase letters only.
outputFormat
Output two integers separated by a space: the length of the longest common subsequence and the number of distinct longest common subsequences.
sample
ABCBDAB
BDCABA
4 3