[UCSD白板题] Longest Common Subsequence of Three Sequences

时间:2023-11-10 08:33:02

Problem Introduction

In this problem, your goal is to compute the length of a longest common subsequence of three sequences.

Problem Description

Task.Given three sequences \(A=(a_1,a_2,\cdots,a_n)\); \(B = (b_1, b_2,\cdots,b_m)\), and \(C=(c_1,c_2,\cdots ,c_l)\), find the length of their longest common subsequence, i.e., the largest non-negative integer \(p\) such that there exists indices
\(1 \leq i_1 < i_2 < \cdots < i_p \leq n\),\(1 \leq j_1 < j_2 < \cdots < j_p \leq m\), \(1 \leq k_1 < k_2 < \cdots < k_p \leq l\)
such that \(a_{i_1}=b_{j_1}=c_{k_1},\cdots ,a_{i_1},b_{j_p},c_{k_p}\).

Input Format.First line: \(n\). Second line: \(a_1,a_2,\cdots,a_n\). Third line: \(m\). Fourth line: \(b_1,b_2,\cdots ,b_m\). Fifth line: \(l\). Sixth line: \(c_1,c_2,\cdots,c_l\)

Constraints.\(1 \leq n,m,l \leq 100\); \(-10^9 < a_i,b_i,c_i < 10^9\)

Output Format. Output \(p\).

Sample 1.
Input:

3
1 2 3
3
2 1 3
3
1 3 5

Output:

2

Sample 2
Input:

5
8 3 2 1 7
7
8 2 1 3 8 10 7
6
6 8 3 1 4 7

Output:

3

Solution