#C1544. Longest Common Subsequence

    ID: 44761 Type: Default 1000ms 256MiB

Longest Common Subsequence

Longest Common Subsequence

Given two strings A and B of lengths \( n \) and \( m \) respectively, your task is to calculate the length of their Longest Common Subsequence (LCS).

The Longest Common Subsequence is defined as the longest sequence that appears in both strings in the same order (but not necessarily contiguously). Formally, for two sequences \( X = \{x_1, x_2, \ldots, x_n\} \) and \( Y = \{y_1, y_2, \ldots, y_m\} \), a sequence \( Z = \{z_1, z_2, \ldots, z_k\} \) is a common subsequence if there exist indices \( 1 \leq i_1 < i_2 < \cdots < i_k \leq n \) and \( 1 \leq j_1 < j_2 < \cdots < j_k \leq m \) such that \( z_l = x_{i_l} = y_{j_l} \) for all \( 1 \leq l \leq k \).

Your implementation should read input from stdin and write the answer to stdout.

inputFormat

The input consists of three lines:

  1. The first line contains two integers \( n \) and \( m \), representing the lengths of the two strings.
  2. The second line contains the string A of length \( n \).
  3. The third line contains the string B of length \( m \).

outputFormat

Output a single integer, the length of the longest common subsequence of the two strings.

## sample
6 6
ABCDEF
AEDFHR
3

</p>