Codeforces Beta Round #10 D. LCIS 动态规划

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

D. LCIS

题目连接:

http://www.codeforces.com/contest/10/problem/D

Description

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 Input

7

2 3 1 6 5 4 6

4

1 3 5 6

Sample Output

3

3 5 6

Hint

题意

给你两个串,求公共最长上升子序列

题解:

考虑暴力枚举第一个串,然后for循环枚举第二个串

dp[i]表示第二个串位置为i的时候与第一个串的的最长公共上升子序列是多少

然后直接暴力更新dp就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;
int dp[maxn],a[maxn],b[maxn],step[maxn],n,m;
void print(int x)
{
if(x==0)return;
print(step[x]);
printf("%d ",b[x]);
}
int main()
{
scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);
scanf("%d",&m);for(int i=1;i<=m;i++)scanf("%d",&b[i]); for(int i=1;i<=n;i++)
{
int pos = 0;
for(int j=1;j<=m;j++)
{
if(a[i]==b[j])
{
dp[j]=dp[pos]+1;
step[j]=pos;
}
else if(a[i]>b[j]&&dp[pos]<dp[j])
pos=j;
}
}
int ans = 0,ans1 = 0;
for(int i=1;i<=m;i++)
if(dp[i]>ans1)
ans1=dp[i],ans=i;
cout<<ans1<<endl;
print(ans);
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

“Codeforces Beta Round #10 D. LCIS 动态规划” 的相关文章

thinkphp如何关闭所有缓存 - 编程语言

这篇文章主要介绍“thinkphp如何关闭所有缓存”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“thinkphp如何关闭所有缓存”文章能帮助大家解决问题。 首先,我们需要了解 ThinkPHP 中的...

【python入门】鸡兔同笼问题

题目: 请编一个程序,用户在同一行内输入两个整数,代表头和脚的数量,编程计算笼中各有多少只鸡和兔,假设鸡和兔都正常,无残疾。如无解则输出Data Error!‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬...

云客服系统有哪些优势?

企业线上的日常服务和运营活动,可能会导致大量的售前、售中和售后问题。客户服务人员面临着大量的重复咨询和高工作量。因此,企业需要使用云客服系统来协助客户服务工作。那么云客服系统有哪些优势呢? 1.全渠道支持,方便消息管理。 云客服系统支持网页、APP、第三方工具等多种接入渠道。可以实现不同平台间的信息...

在服务器上怎么使用PHP彻底删除文件 - 编程语言

本篇内容主要讲解“在服务器上怎么使用PHP彻底删除文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“在服务器上怎么使用PHP彻底删除文件”吧! 步骤1:查找要删除的文件首先,我们需要指定要删除的...

Python

检查  ...\Lib\site-packages\Django-1.10.2-py2.7.egg\django\conf\locale下无zh-cn文件夹,有zh-Hans和zh-Hant两个文件, 其中 zh-Hans是简体中文  ...

HDU 1072Nightmare(BFS+剪枝)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1072这题好不容易调试成功了(因为各种手残。。),结果提交上去是TLE。。于是自己想剪枝方法,但是没想出来。。于是睡了个觉。。醒来还是没想出来。。。而且毫无剪枝思路。。。于是找了找网上的题解,才明白是怎样...