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);
}

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

  1. Codeforces Beta Round #10 D. LCIS

    题目链接: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limi ...

  2. Codeforces Beta Round #10 D. LCIS(DP&amp;LCIS)

    D. LCIS time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  3. Codeforces Beta Round #10 C. Digital Root 数学

    C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...

  4. Codeforces Beta Round #10 B. Cinema Cashier 暴力

    B. Cinema Cashier 题目连接: http://www.codeforces.com/contest/10/problem/B Description All cinema halls ...

  5. Codeforces Beta Round #10 A. Power Consumption Calculation 水题

    A. Power Consumption Calculation 题目连接: http://www.codeforces.com/contest/10/problem/A Description To ...

  6. Codeforces Beta Round #10 B. Cinema Cashier (树状数组)

    题目大意: n波人去k*k的电影院看电影. 要尽量往中间坐,往前坐. 直接枚举,贪心,能坐就坐,坐在离中心近期的地方. #include <cstdio> #include <ios ...

  7. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  8. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  9. Codeforces Beta Round 84 (Div. 2 Only)

    layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: ...

随机推荐

  1. PHP分页类分享

    /** * 获取分页的HTML内容 * @param integer $page 当前页 * @param integer $pages 总页数 * @param string $url 跳转url地 ...

  2. thinkphp博客项目纪录

    项目地址:http://files.cnblogs.com/files/wordblog/blog.zip

  3. MySQL常见错误代码说明

    附:MySQL常见错误代码说明 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导致删除数据 ...

  4. html的loadrunner脚本2

    Action(){ char buf[1911]; //¶¨Òå×Ö·ûÊý×飬Ö÷ÒªÓÃÓÚдÈëXML±¨Îĵ½»º³åÇø char str_Body[4086]; //³Ð½Ó±¨Î ...

  5. sed的额外用法(网摘)

    #在我开始动手写一个一个的脚本的时候才会看到更多的用法 1. 在某行的前一行或后一行添加内容(前提是要确定行的内容) # 匹配行前加 sed -i '/allow/ideny' httpd.conf ...

  6. Linux文件访问流程及磁盘inode和block总结

    Linux文件访问流程 inode是文件的唯一标识,文件名和inode的对应关系存放在上一级目录的block中:inode里有指向文件block的指针和文件的属性,从而通过block获得文件数据. 磁 ...

  7. Linux shell中运行命令后加上字符“&”的作用(转)

    原文链接为:http://blog.sina.com.cn/s/blog_963453200102uya7.html & 放在启动参数后面表示设置此进程为后台进程 默认情况下,进程是前台进程, ...

  8. P3960 列队

    这是NOIP 2017最后一道题 不知道这道题有没有人代码写的和我一样麻烦. Solution 30分暴力 维护每行每列的元素. 每次删除一个元素的时候, 需要修改一行一列 因此复杂度上界\(O(nm ...

  9. 关于HTML&CSS的笔记

    最近在看Jon Duckett的HTML&CSS一书(http://book.douban.com/subject/6585090/),书的排版和讲解方式很不错,并且有许多其他教学材料遗漏的关 ...

  10. vue框架muse-ui官网文档主题错误毕竟【01】

    在使用了element-ui后,总觉得不尽兴,再学一个响应式的muse-ui发现是个小众框架,但是我很喜欢. 指出官网文档里的主题使用描述错误. 首先,在vue-cli里安装raw-loader:np ...