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

  定义dp[i][j]表示字符串a前i个和字符串b的前j个且以b[j]结尾构成的最长公共上升子序列的长度,定义一个max用来保存最大的长度。用两个循环,外层循环控制字符串a,内层循环控制字符串b。如果a[i]不等于b[j],则dp[i][j]=dp[i-1][j];如果a[i]大于b[j]而且max<dp[i-1][j],则max=dp[i-1][j];如果a[i]等于b[j],则dp[i][j]=max+1。最后的答案在dp[n][1~m]中最大的。(注意,这种情况是字符串都是从下标为1开始存的)

  下面是例题:  杭电1423

Greatest Common Increasing Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3482    Accepted Submission(s):
1098

Problem Description
This is a problem from ZOJ 2432.To make it easyer,you
just need output the length of the subsequence.
 
Input
Each sequence is described with M - its length (1 <=
M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence
itself.
 
Output
output print L - the length of the greatest common
increasing subsequence of both sequences.
 
Sample Input
1
5
1 4 2 5 -12
4
-12 1 2 4
 
Sample Output
2
 
代码如下:
 #include <iostream>
using namespace std;
int t,n,m;
int a[],b[];
int dp[][]; int LCIS()
{
int i,j;
int max;
for(i=;i<=n;i++)
{
max = ;
for(j=;j<=m;j++)
{
dp[i][j] = dp[i-][j];
if (a[i]>b[j] && max<dp[i-][j])
max = dp[i-][j];
if(a[i] == b[j])
dp[i][j] = max + ;
}
}
max = ;
for(i=;i<=m;i++)
if(max<dp[n][i])
max = dp[n][i];
return max;
} int main()
{
int i,j;
cin>>t;
while(t--)
{
cin>>n;
for(i=;i<=n;i++)
cin>>a[i];
cin>>m;
for(j=;j<=m;j++)
cin>>b[j];
memset(dp,,sizeof(dp));
cout<<LCIS()<<endl;
if (t)
cout<<endl;
}
}

  其实还有一种更牛的方法是采用一维数组,但是时间还是n^2。当i循环到k的时候,原来dp[i]表示原来的dp[k][j],因为当a[i]!=b[j]的时候dp[i]的值是不变的,沿用过去的值就行了,只有当a[i]==b[j]的时候才需要更新dp[i]的值。

代码如下:

 #include <iostream>
using namespace std;
int t,n,m;
int a[],b[];
int dp[]; int LCIS()
{
int i,j;
int max;
for(i=;i<=n;i++)
{
max = ;
for(j=;j<=m;j++)
{
if (a[i]>b[j] && max<dp[j])
max = dp[j];
if(a[i] == b[j])
dp[j] = max + ;
}
}
max = ;
for(i=;i<=m;i++)
if(max<dp[i])
max = dp[i];
return max;
} int main()
{
int i,j;
cin>>t;
while(t--)
{
cin>>n;
for(i=;i<=n;i++)
cin>>a[i];
cin>>m;
for(j=;j<=m;j++)
cin>>b[j];
memset(dp,,sizeof(dp));
cout<<LCIS()<<endl;
if (t)
cout<<endl;
}
}

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

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

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

  2. hdu 1423 最长公共递增子序列 LCIS

    最长公共上升子序列(LCIS)的O(n^2)算法 预备知识:动态规划的基本思想,LCS,LIS. 问题:字符串a,字符串b,求a和b的LCIS(最长公共上升子序列). 首先我们可以看到,这个问题具有相 ...

  3. 动态规划——最长公共上升子序列LCIS

    问题 给定两个序列A和B,序列的子序列是指按照索引逐渐增加的顺序,从原序列中取出若干个数形成的一个子集,若子序列的数值大小是逐渐递增的则为上升子序列,若A和B取出的两个子序列A1和B1是相同的,则A1 ...

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

    Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...

  5. LCIS最长公共上升子序列

    最长公共上升子序列LCIS,如字面意思,就是在对于两个数列A和B的最长的单调递增的公共子序列. 这道题目是LCS和LIS的综合. 在LIS中,我们通过两重循环枚举当序列以当前位置为结尾时,A序列中当前 ...

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

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

  7. LCIS 最长公共上升子序列问题DP算法及优化

    一. 知识简介 学习 LCIS 的预备知识: 动态规划基本思想, LCS, LIS 经典问题:给出有 n 个元素的数组 a[] , m 个元素的数组 b[] ,求出它们的最长上升公共子序列的长度. 例 ...

  8. CF10D LCIS 最长公共上升子序列

    题目描述 This problem differs from one which was on the online contest. The sequence a1,a2,...,an a_{1}, ...

  9. 【线型DP模板】最上上升子序列(LIS),最长公共子序列(LCS),最长公共上升子序列(LCIS)

    BEGIN LIS: 一个数的序列bi,当b1 < b2 < … < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, …, aN),我们可以得到一些上升的子序 ...

随机推荐

  1. 七、考反映小游戏《苹果iOS实例编程入门教程》

    该app为应用的功能为一个简单的考反应游戏 纲要:-UIButton, UILabel, UIImageView 的运用:-利用rendom增加游戏可玩性: 游戏说明: 在按下开始游戏后,分为三盏的指 ...

  2. OSG入门即osgEarth建立一个地球的详细步骤

    OSG入门即osgEarth建立一个地球的详细步骤 转:http://blog.csdn.net/xiaol_deng/article/details/9246291 最近在学习有关osg的知识,刚开 ...

  3. C# Double保留小数点后面位数

    C# Double保留小数点后面位数 有的时候我们要对一些数据进行百分比的操作.一般的数据我们只要用 .ToString("P")就可以得到小数点后2位的百分比.而且是自动 加上% ...

  4. ios修改产品名

    在创建项目的时候,会设置一个项目名,以后生成的APP名字也就是这个了,但由于某种原因,我想修改APP名字,也就是屏幕程序图标下面显示的那个,这该怎么办呢? 下面有三种方法都可以: 修改Product ...

  5. [CareerCup] 18.13 Largest Rectangle of Letters

    18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...

  6. Hadoop.2.x_高级应用_二次排序及MapReduce端join

    一.对于二次排序案例部分理解 1. 分析需求(首先对第一个字段排序,然后在对第二个字段排序) 杂乱的原始数据 排序完成的数据 a,1 a,1 b,1 a,2 a,2 [排序] a,100 b,6 == ...

  7. js在输出时乱码

    如果网页头是<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> ...

  8. Amoeba-mysql读写分离实战

    Amoeba-mysql读写分离实战 Amoeba用途有很多,这里看标题我们就先说读写分离,因为我也只会这个.Amoeba定义为国内的,开源的.目前(2015年10月20日)我们用amoeba2.2版 ...

  9. jenkins使用deploy-plugin自动构建部署war包

    jenkins+ant+maven+tomcat 1安装 jenkins 使用yum安装的 # 下载库 wget -O /etc/yum.repos.d/jenkins.repo http://pkg ...

  10. 会场安排问题---nyoj14

    描述学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工作就是安排学校小礼堂的活动,每个时间最多安排一个活动.现在小刘有一些活动计划的时间表,他想尽 ...