#C14441. Longest Common Subsequence in Unsorted Arrays

    ID: 44091 Type: Default 1000ms 256MiB

Longest Common Subsequence in Unsorted Arrays

Longest Common Subsequence in Unsorted Arrays

Given two unsorted arrays of integers, your task is to first sort each array in non-decreasing order and then find their Longest Common Subsequence (LCS). In this context, the LCS is defined as the longest sequence of integers that appear in both sorted arrays. Formally, if the two arrays are denoted by \(A\) and \(B\), you are to compute a sequence \(S\) such that \(S\) is a subsequence of both \(sorted(A)\) and \(sorted(B)\), and \(|S|\) is maximized.

The answer should include the LCS (with elements printed in non-decreasing order) and its length.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains an integer \(n\), the number of elements in the first array.
  • The second line contains \(n\) space-separated integers representing the first array.
  • The third line contains an integer \(m\), the number of elements in the second array.
  • The fourth line contains \(m\) space-separated integers representing the second array.

outputFormat

Output to standard output (stdout) in the following format:

  • The first line should contain the common subsequence's elements in non-decreasing order separated by a single space. If there is no common element, output an empty line.
  • The second line should output the length of the subsequence.
## sample
4
3 10 4 5
4
3 10 4 5
3 4 5 10

4

</p>