题目描述 Description

熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目。小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序列了。
小沐沐说,对于两个串A,B,如果它们都包含一段位置不一定连续的数字,且数字是严格递增的,那么称这一段数字是两个串的公共上升子串,而所有的公共上升子串中最长的就是最长公共上升子串了。
奶牛半懂不懂,小沐沐要你来告诉奶牛什么是最长公共上升子串。不过,只要告诉奶牛它的长度就可以了。

输入描述 Input Description

第一行N,表示A,B的长度。
第二行,串A。
第三行,串B。

输出描述 Output Description

输出长度。

样例输入 Sample Input

4
2 2 1 3
2 1 2 3

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

1<=N<=3000,A,B中的数字不超过maxlongint

/*
首先是n^3的做法,f[i][j]表示以a串的前i个整数与b串的前j个整数且以b[j]为结尾构成的LCIS的长度。转移就很好想了。
*/
#include<cstdio>
#include<iostream>
#define N 3010
using namespace std;
int a[N],b[N],f[N][N],n;
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
f[i][j]=f[i-][j];
if(a[i]==b[j]){
int maxn=;
for(int k=;k<j;k++){
if(b[k]<b[j])maxn=max(maxn,f[i-][k]);
}
f[i][j]=max(f[i][j],maxn+);
}
}
int ans=;
for(int i=;i<=n;i++)ans=max(ans,f[n][i]);
printf("%d",ans);
return ;
}
/*
然后是n^2的做法,考虑优化n^3。我们可以考虑省去第三重循环,因为每次取的都是最大值,所以可以维护一个最大值,可以用一个数组维护。但是题解给出了更好的方法:因为只有b[k]<b[j]时才会更新,而且a[i]=b[j]时才用到,所以考虑b[k]<a[i]时维护一个最大值。
*/
#include<cstdio>
#include<iostream>
#define N 3010
using namespace std;
int a[N],b[N],f[N][N],n;
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
for(int i=;i<=n;i++){
int maxn=;
for(int j=;j<=n;j++){
f[i][j]=f[i-][j];
if(a[i]>b[j])maxn=max(maxn,f[i-][j]);
if(a[i]==b[j])f[i][j]=maxn+;
}
}
int ans=;
for(int i=;i<=n;i++)ans=max(ans,f[n][i]);
printf("%d",ans);
return ;
}

最长公共上升子序列(codevs 2185)的更多相关文章

  1. codevs 2185 最长公共上升子序列

    题目链接: codevs 2185 最长公共上升子序列codevs 1408 最长公共子序列 题目描述 Description熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升 ...

  2. codevs 2185 最长公共上升子序列--nm的一维求法

    2185 最长公共上升子序列  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 钻石 Diamond 题目描述 Description 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目 ...

  3. Codevs 2185【模板】最长公共上升子序列

    题目描述 Description 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序列了.小沐沐说,对于 ...

  4. 最长公共上升子序列(LCIS)

    最长公共上升子序列慕名而知是两个字符串a,b的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...

  5. ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

    Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  6. POJ 2127 最长公共上升子序列

    动态规划法: #include <iostream> #include <cstdio> #include <fstream> #include <algor ...

  7. [CodeForces10D]LCIS(最长公共上升子序列) - DP

    Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...

  8. 最长递增子序列(lis)最长公共子序列(lcs) 最长公共上升子序列(lics)

    lis: 复杂度nlgn #include<iostream> #include<cstdio> using namespace std; ],lis[],res=; int ...

  9. [ACM_动态规划] UVA 12511 Virus [最长公共递增子序列 LCIS 动态规划]

      Virus  We have a log file, which is a sequence of recorded events. Naturally, the timestamps are s ...

随机推荐

  1. Ubuntu 14 编译安装 XDebug - 2.3.3 For PHP - 5.4.45

    安装过程如下: 1.下载XDebug源码:http://xdebug.org/files/xdebug-2.3.3.tgz 2.解压到某个目录,如 /opt/software/xdebug-2.3.3 ...

  2. Swift2.1 语法指南——泛型

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  3. 全文检索引擎Solr系列——整合MySQL、MongoDB

    MySQL 拷贝mysql-connector-java-5.1.25-bin.jar到E:\solr-4.8.0\example\solr-webapp\webapp\WEB-INF\lib目录下面 ...

  4. C#操作word模板插入文字、图片及表格详细步骤

    c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...

  5. BZOJ4514——[Sdoi2016]数字配对

    有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci×cj 的 ...

  6. Distinct Subsequences Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  7. 2016年11月3日--Window.document对象

    五.相关元素操作: var a = document.getElementById("id");找到a: var b = a.nextSibling,找a的下一个同辈元素,注意包含 ...

  8. JQ引用

    <script type="text/javascript" src="http://files.cnblogs.com/914556495wxkj/jquery- ...

  9. Unity3d运行时动态修改材质

    void Start () { const string MainTexVariableName = "_MainTex"; var renders = gameObject.Ge ...

  10. jquery checkbox 限制多选的个数

    2015年11月6日 16:32:49 选中第四个的时候提示超过了3个, 点解alert框取消后, 将最后一个选中的checkbox取消选中 <script> $(document).re ...