Greatest Common Increasing Subsequence

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1432

题目大意:给出两串数字,求他们的最长公共上升子序列(LCIS),并且打印出来。

Sample Input

1

5
1 4 2 5 -12
4
-12 1 2 4

Sample Output

2
1 4

分析:神奇就神奇在是LIS与LCS的组合

令dp[i][j]表示A串的前i个,与B串的前j个,并以B[j]为结尾的LCIS 的长度.

状态转移方程:

  f(A[i]==B[j])   dp[i][j]=max(dp[i-1][k])+1;  ( 1 <= k < j )

  else   dp[i][j]=dp[i-1][j];

然后选择循环顺序,就可以将算法的复杂度降为n*n.

代码如下:

 /*这个代码结果虽然对,跟样例的输出都不一样,而且两个输出数据之间有空行都没有实现,却能AC,有点匪夷所思*/
# include<stdio.h>
# include<string.h>
#define MAX 550 struct node{
int x,y;
}path[MAX][MAX]; int dp[MAX][MAX];
int s[MAX],t[MAX]; int main(){
int T,i,j;
scanf("%d",&T);
while(T--)
{
memset(path,,sizeof(path));
int n,m;
scanf("%d",&n);
for(i=; i<=n; i++)
scanf("%d",&s[i]);
scanf("%d",&m);
for(i=; i<=m; i++)
scanf("%d",&t[i]);
memset(dp,,sizeof(dp));
int max = ;
for(i=; i<=n; i++)
{
max = ;
int tx = ,ty = ;
for(j=; j<=m; j++)
{
dp[i][j] = dp[i-][j];
path[i][j].x = i-;
path[i][j].y = j;
if( s[i] > t[j] && max < dp[i-][j])
{
max = dp[i-][j];
tx = i-;
ty = j;
}
if( s[i] == t[j] )
{
dp[i][j] = max+;
path[i][j].x = tx;
path[i][j].y = ty;
}
}
}
max = -;
int id;
for(i=; i<=m; i++)
if(dp[n][i]>max)
{
max = dp[n][i];
id = i;
}
int save[MAX];
int cnt=;
int tx,ty;
tx=n; ty=id;
while(dp[tx][ty] != )
{
int tmpx,tmpy;
tmpx = path[tx][ty].x;
tmpy = path[tx][ty].y;
if(dp[tx][ty] != dp[tmpx][tmpy])
{
save[cnt++]=t[ty];
}
tx = tmpx; ty = tmpy;
}
printf("%d\n",max);
for(i=cnt-; i>=; i--)
printf("%d ",save[i]);
printf("\n");
}
return ;
}

ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)的更多相关文章

  1. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

  2. $ZOJ\ 2432\ Greatest\ Common\ Increasing\ Subsequence$

    传送门 $Description$ 求两个序列的最长公共上升子序列 $Solution$ $f[i][j]$表示$a$序列匹配到$i$和$b$序列匹配到$j$的最长上升序列的长度,这里并不要求$a[i ...

  3. LCIS POJ 2172 Greatest Common Increasing Subsequence

    题目传送门 题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列 分析:a[i] != b[j]: dp[i][j] = dp[i-1][j ...

  4. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  5. HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  6. 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)

    \(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...

  7. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  8. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

  9. HDU1423:Greatest Common Increasing Subsequence(LICS)

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

随机推荐

  1. jQuery获取属性之自己遇到的问题

    刚开始是这种写法  用的 attr  结果获取不到 if($("#reg_username_span").attr("display") == 'block') ...

  2. 编程之美 两个叶子的节点之间 最大距离 变种 leecode

    提交地址: https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 说一下思路http://www.cnblogs.com/mil ...

  3. 白帽子讲Web安全2.pdf

    XSS构造技巧 利用字符编码: var redirectUrl="\";alert(/XSS/);"; 本身没有XSS漏洞,但由于返回页面是GBK/GB2312编码的“% ...

  4. zabbix邮件告警

    Zabbix邮件告警看了很多文档,写的那叫一个蛋疼,明明没有发出去邮件,硬要糊弄观众,我也跟着被糊弄. 操作系统环境: CentOS 5.5 x84_64位 Zabbix版本2.2.3 Web服务器: ...

  5. python-面向对象(三)——类的特殊成员

    类的特殊成员 1. __doc__     表示类的描述信息 class Foo: """ 描述类信息,这是用于看片的神奇 """ def ...

  6. 4 weekend110的hive入门

    查看企业公认的最新稳定版本:       https://archive.apache.org/dist/  Hive和HBase都很重要,当然啦,各自也有自己的替代品. 在公司里,SQL有局限,大部 ...

  7. 关于list、set、map的几点总结

    用法: 1. 如果涉及到堆栈,队列等操作,应该考虑用List, 对于需要快速插入,删除元素,应该使用LinkedList, 如果需要快速随机访问元素,应该使用ArrayList.2. 如果程序在单线程 ...

  8. web dynpro message(备忘用)

    DATA lo_api_controller TYPE REF TO if_wd_controller. DATA lo_message_manager TYPE REF TO if_wd_messa ...

  9. ABAP(笔记)

    1.excel表格上传 *&---------------------------------------------------------------------* ** 程序名称:ZSD ...

  10. tabhost中setup()和setup(LocalActivityManager activityGroup)

    如果用系统默认的tabhost时, 直接用getTabhost()初始化,整个类继承tabActivity. 当没有选择系统tabhost默认id时,而是自己定义的id时,必须使用 findViewB ...