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 <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; int n,m,a[505],b[505],dp[505][505]; int LICS()
{
int MAX,i,j;
memset(dp,0,sizeof(dp));
for(i = 1; i<=n; i++)
{
MAX = 0;
for(j = 1; j<=m; j++)
{
dp[i][j] = dp[i-1][j];
if(a[i]>b[j] && MAX<dp[i-1][j])
MAX = dp[i-1][j];
if(a[i]==b[j])
dp[i][j] = MAX+1;
}
}
MAX = 0;
for(i = 1; i<=m; i++)
if(MAX<dp[n][i])
MAX = dp[n][i];
return MAX;
} int main()
{
int i,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i = 1; i<=m; i++)
scanf("%d",&b[i]);
printf("%d\n",LICS());
if(t)
printf("\n");
} return 0;
}

上面的虽然可以解决,但是二维浪费空间较大,我们注意到在LICS函数中有一句dp[i][j] = dp[i-1][j],这证明dp数组前后没有变化!于是可以优化成一维数组!

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[505],b[505],dp[505],n,m; int LICS()
{
int i,j,MAX;
memset(dp,0,sizeof(dp));
for(i = 1; i<=n; i++)
{
MAX = 0;
for(j = 1; j<=m; j++)
{
if(a[i]>b[j] && MAX<dp[j])
MAX = dp[j];
if(a[i]==b[j])
dp[j] = MAX+1;
}
}
MAX = 0;
for(i = 1; i<=m; i++)
if(MAX<dp[i])
MAX = dp[i];
return MAX;
} int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i = 1; i<=m; i++)
scanf("%d",&b[i]);
printf("%d\n",LICS());
if(t)
printf("\n");
} return 0;
}

HDU1423:Greatest Common Increasing Subsequence(LICS)的更多相关文章

  1. HDU1423:Greatest Common Increasing Subsequence

    浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html 题目传送门:http://acm.hdu.edu.cn/showproblem.php?p ...

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

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

  3. Greatest Common Increasing Subsequence hdu1423

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

  4. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

  5. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

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

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

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

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

  8. POJ 2127 Greatest Common Increasing Subsequence

    You are given two sequences of integer numbers. Write a program to determine their common increasing ...

  9. HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)

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

随机推荐

  1. ios 调节器 modal 得知

    代码中创建: 1.appdelegate 该contoller放置controller下一个 - (BOOL)application:(UIApplication *)application didF ...

  2. 教你Ant安装和配置

    Ant它是基于Java施工工具,它的主要作用是产生能够运行Java计划,把握Ant一些功能可以使项目更专业. 忙乱,可以在这里下载2014年8最近一个月Ant http://download.csdn ...

  3. HDU1203_I NEED A OFFER!【01背包】

    I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. crawler_网络爬虫中编码的正确处理与乱码的解决策略

    转载: http://hi.baidu.com/erliang20088/item/9156132bdaeae8949c63d134 最近一个月一直在对nutch1.6版进行中等层次的二次开发,本来是 ...

  5. WCF中的数据契约(DataContract)

    服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被序列化在服务端和客户端之间传送,如下所 ...

  6. update值与原值相同时,SQL Server会真的去update还是忽略呢?

    原文:update值与原值相同时,SQL Server会真的去update还是忽略呢? 考虑下面的情况: 当update值与原值相同时,SQL Server会真的去update还是忽略?例如: upd ...

  7. 私人定制javascript中数组小知识点(Only For Me)

    先上笑话,1.刚看到一个游泳的,想起公司组织去三亚旅游,老板跳海里,各种挣扎,捞上来老板第一句话:我记得我会游泳的啊. 2.媳妇说:老公对不起,我把你新买的自行车撞散架了! 老公:没事宝贝,你若安好, ...

  8. HammerDB数据库压力工具使用简略步骤

    欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/38879681 HammerDB数据库压力工具使用简略步骤 尽管没有图,可是文字 ...

  9. IOS启动其他应用程序

    从app1打开app2.主要的思路就是,能够为app2定义一个URL,在app1中通过打开这个URL来打开app2,在此过程中.能够传送一些參数. 在app1的代码中打开刚才定义的URL.代码例如以下 ...

  10. Socket 学习(一)

    本次项目增加的引用using System.Net; using System.Net.Sockets; using System.Threading; TextBox.CheckForIllegal ...