#P2516. Longest Common Subsequence and Count

    ID: 15786 Type: Default 1000ms 256MiB

Longest Common Subsequence and Count

Longest Common Subsequence and Count

Given two character sequences X={x0,x1,,xm1}X=\{x_0,x_1,\cdots,x_{m-1}\} and Y={y0,y1,,yn1}Y=\{y_0,y_1,\cdots,y_{n-1}\}, a subsequence of a sequence is formed by deleting zero or more characters (not necessarily contiguous) from the sequence. A sequence ZZ is a subsequence of XX if there exists a strictly increasing index sequence {i0,i1,,ik1}\{i_0,i_1,\cdots,i_{k-1}\} such that for every j=0,1,,k1j=0,1,\cdots,k-1, we have xij=zjx_{i_j}=z_j. For example, if X=ABCBDABX=\tt{ABCBDAB}, then BCDB\tt{BCDB} is a subsequence of XX.

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 LL 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 SS such that each SS is common to both sequences and has length LL.

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