LCIS ——最长公共上升子序列

This problem differs from one which was on the online contest.

The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n.

The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements.

You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.

Input

The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.

Output

In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.

Sample 1

InputcopyOutputcopy
7
2 3 1 6 5 4 6
4
1 3 5 6
3
3 5 6 

Sample 2

InputcopyOutputcopy
5
1 2 0 2 1
3
1 0 1
2
0 1 
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 1e3 + 10, INF = 2e9;
int n, m, num1, num2;
int a[N];
int b[N];
int dp[N][N]; // 前i,j个数中,且以B[j]结尾的公共最长子序列集合
int pre[N];
void output(int x)
{
    if (!x)
        return;
    output(pre[x]);
    cout << b[x] << " ";
    // 迭代输出
}
void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    cin >> m;
    for (int i = 1; i <= m; i++)
    {
        cin >> b[i];
    }
    for (int i = 1; i <= n; i++)
    {                 // 枚举a
        int maxv = 1; // 表示前缀最大值;
        int flag = 0;
        for (int j = 1; j <= m; j++) // 枚举m
        {
            dp[i][j] = dp[i - 1][j]; // a[i]和a[j]不相等即,a[i]不包含在LCS中
            // 相等才更新dp[i][j];
            if (a[i] == b[j])
            {
                if (dp[i][j] < maxv)
                {
                    dp[i][j] = maxv;
                    pre[j] = flag;
                }
            }
            // 更新前缀最大值;
            // 所有小于a[i]的值中的最大值;当j循环到m的时候就已经在更新了
            if (b[j] < a[i])
            {
                if (maxv < dp[i - 1][j] + 1)
                {
                    maxv = dp[i - 1][j] + 1;
                    flag = j;
                    // 逐个记录前驱
                }
            }
        }
    }
    int res = 0;
    int pos = 0;
    for (int i = 1; i <= m; i++)
    {
        if (res < dp[n][i])
        {
            res = dp[n][i];
            pos = i; // 由于需要递归输出
        }
    }
    cout << res << endl;
    output(pos);
}

signed main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
    while (T--)
    {
        solve();
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ou_fan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值