7038.Permutation Subsequence

Time Limit: 1s Memory Limit: 512MB

Given two arrays which are permutations, find their longest common subsequence.

A subsequence is a sequence of array elements from left to right that can contain gaps. A common subsequence is a subsequence that appears in both arrays.

Input Format(From the terminal/stdin)

The first line has two integers $n$ and $m$ : the sizes of the arrays.

The second line has $n$ integers $a_1,a_2,\dots,a_n$ : the contents of the first array.

The third line has $m$ integers $b_1,b_2,\dots,b_m$ : the contents of the second array.

  • $1 \le n,m \le 2 \cdot 10^5$
  • $1 \le a_i \le n$
  • $1 \le b_i \le m$

Output Format(To the terminal/stdout)

First print the length of the longest common subsequence.

After that, an example of such a sequence. If there are several solutions, you can print any of them.

Sample Input

Copy
8 6
3 1 2 8 5 7 6 4
6 5 1 2 3 4
 · \n
 · · · · · · · \n
 · · · · · \n

Sample Output special judge

Copy
3
1 2 4
 \n
 · · \n
Source: CSES, Additional Problems I, 3404

Submit

请先 登录

© 2025 FAQs