最长公共上升子序列慕名而知是两个字符串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面试题及答案-转载

    1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答: Object-c的类不可以多重继承:可以实现多个接口,通过实现 ...

  2. FS_11C14温湿度传感器(二)

    作者:刘老师,华清远见嵌入式学院讲师. 在FS_11C14平台DHT11传感器程序: /******************************************************** ...

  3. Hightcharts动态创建series

    第一种方法: 申明options时动态设置series,然后再创建chart对象 代码如下: <html> <head> <title>Highcharts Exa ...

  4. jquery插件之拖拽

    该插件乃本博客作者所写,目的在于提升作者的js能力,也给一些js菜鸟在使用插件时提供一些便利,老鸟就悠然地飞过吧. 此插件旨在实现目前较为流行的拖拽效果,您可以根据自己的实际需求来设置被拖拽元素是否可 ...

  5. 1055. The World's Richest (25)

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  6. Load Mental Ray in Maya 2015

    In Maya 2015, we usually use mental ray to render our model, some new users may not see the mental r ...

  7. Git基本命令行操作

    A. 新建Git仓库,创建新文件夹git init  B. 添加文件到git索引git add <filename>  --- 单个文件添加git add * --- 全部文件添加 C. ...

  8. daterangepicker 日期范围插件自定义 可选 年份

    minDate:'01/01/2012',maxDate:'01/01/2015' $("#txtPODate").daterangepicker({ singleDatePick ...

  9. sign in和sign up区别

    如果是网站的话sign up是注册,sign in是登录的意思,另外,sign out退出

  10. iOS Mail.app inject kit

    from:https://github.com/jansoucek/iOS-Mail.app-inject-kit 测试机:iOS 8.2 发一封邮件. Apple ID的重要性不言而喻,这种钓鱼手法 ...