#C8032. Longest Common Subsequence

    ID: 51970 Type: Default 1000ms 256MiB

Longest Common Subsequence

Longest Common Subsequence

The problem is to compute the length of the Longest Common Subsequence (LCS) of two sequences of integers. Given two sequences, the LCS is defined as the longest sequence that appears in both of them, not necessarily consecutively.

For two sequences \( A = [a_1, a_2, \dots, a_n] \) and \( B = [b_1, b_2, \dots, b_m] \), the LCS length is the maximum \( k \) such that there exists indices \( 1 \leq i_1 < i_2 < \dots < i_k \leq n \) and \( 1 \leq j_1 < j_2 < \dots < j_k \leq m \) with \( a_{i_t} = b_{j_t} \) for all \( t=1,2,\dots,k \).

Your task is to implement an algorithm that reads two sequences from the standard input and outputs the length of their LCS.

inputFormat

The input is given via standard input (stdin) and consists of four lines:

  1. The first line contains an integer \( n \), the length of the first sequence.
  2. The second line contains \( n \) space-separated integers representing the first sequence.
  3. The third line contains an integer \( m \), the length of the second sequence.
  4. The fourth line contains \( m \) space-separated integers representing the second sequence.

outputFormat

Output a single integer to standard output (stdout): the length of the longest common subsequence of the given two sequences.

## sample
6
1 3 4 1 2 3
5
2 1 3 4 1
4

</p>