#T247. 最长公共子上升序列

最长公共子上升序列

Description

Given two integer sequences, write a program to find their longest increasing common subsequence.

A sequence S1,S2,...,SNS_1, S_2, ..., S_N of length NN is called an increasing subsequence of a sequence A1,A2,...,AMA_1, A_2, ..., A_M of length MM when the following conditions are satisfied:
There exist indices 1i1<i2<...<iNM1 \leq i_1 < i_2 < ... < i_N \leq M such that for all 1jN1 \leq j \leq N, Sj=AijS_j = A_{i_j}, and for all 1j<N1 \leq j < N, Sj<Sj+1S_j < S_{j+1}.

Input Format

Each sequence is represented by two lines. The first line contains the length MM (1M5001 \leq M \leq 500), and the second line contains the MM integers AiA_i (231Ai<231-2^{31} \leq A_i < 2^{31}) of the sequence.

Output Format

On the first line, output the length LL of the longest increasing common subsequence of the two sequences. On the second line, output the subsequence. If there are multiple valid subsequences, any one may be printed.

5
1 4 2 5 -12
4
-12 1 2 4

2
1 4

Hint

Classic algorithm Baidu search, deeply comprehend.