Greatest Common Increasing Subsequence hdu1423 最长公共递增子序列
来源:互联网 发布:淘宝网2016下载安装 编辑:程序博客网 时间:2024/05/01 16:44
http://acm.hdu.edu.cn/showproblem.php?pid=1423
下面是从前辈那里copy来的最长公共递增子序列的详解。
给出两个序列a和b,求序列的最长公共递增子序列
例如 a:1,4,2,5,-12 b:-12,1,2,4,最长的公共递增子序列是2(序列为2,4)。
解决方案:
用一个二维数组f[i][j]表示a序列的前i项,b序列的前j项,并且以b[j]结束的LCIS的长度。
则有:
当a[i] != b[j]时,f[i][j]=f[i-1][j]; //如果最后一项不等,那么f[i][j]必定与f[i-1][j]或者f[i][j-1]相等。
当a[i] == b[j]时,f[i][j]=max(f[i-1][k])+1,1<=k<=j-1 && b[j]>b[k]; //求f[i-1]行中的最长,并且保证新增的数大于已经有的数
但是这样的效率为O(n^3)。 不难发现,求f[i-1][k]的时候可以通过一个变量temp来解决,当k从1遍历到j的同时,可以不断更新temp的值,如果a[i]>b[j]时,令temp=max(temp,f[i-1][j]);如果a[i]==b[j]时,令f[i][j]=max+1。
#define N 1010int a[N],b[N];int f[N][N];int LCIS(int a[],int b[],int lena,int lenb){ int i,j,temp; memset(f,0,sizeof(f)); for(i=1;i<=lena;i++) { for(j=1,temp=0;j<=lenb;j++) { f[i][j]=f[i-1][j]; if(a[i]>b[j] && f[i-1][j]>temp) temp=f[i-1][j]; if(a[i] == b[j]) f[i][j]=temp+1; } } temp=0; for(i=1;i<=lenb;i++) if(f[lena][i] > temp) temp=f[lena][i]; return temp;}f数组也可以改成一维数组,减少空间。f[i]表示原来的f[i][j],f[i]的值可以沿用上一次循环的值,所以原来代码中的f[i][j]=f[i-1][j]可以省略。
int f[N];int LCIS(int a[],int b[],int lena,int lenb){ int i,j,temp; for(i=1;i<=lena;i++) { for(j=1,temp=0;j<=lenb;j++) { if(a[i]>b[j] && f[j]>temp) temp=f[j]; if(a[i]==b[j]) f[j]=temp+1; } } temp=0; for(i=1;i<=lenb;i++) if(f[i]>temp) temp=f[i]; return temp;}
1432是一道单纯套公式的题目,但是题目没有给的描述是首先输入一个数表示测试用例的数量,然后每组测试用例之间有一行空行。
#include <iostream>using namespace std;#define NLEN 505int a[NLEN],b[NLEN],f[NLEN][NLEN];int main(){#ifndef ONLINE_JUDGEfreopen("1423in.txt","r",stdin);#endifint t,n,m,i,j,temp,ce=0;scanf("%d",&t);while (t--){ce++;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]);for (i=0;i<=n;i++)f[i][0]=0;for (j=0;j<=m;j++)f[0][j]=0;for (i=1;i<=n;i++)for (j=1,temp=0;j<=m;j++){f[i][j]=f[i-1][j];if (a[i]>b[j]&&f[i-1][j]>temp)//更新Temptemp=f[i-1][j];if (a[i]==b[j])f[i][j]=temp+1;}for (j=0;j<=m;j++)if (f[n][j]>temp)temp=f[n][j];if (ce!=1)putchar(10);printf("%d\n",temp);}return 0;}
- Greatest Common Increasing Subsequence hdu1423 最长公共递增子序列
- hdu1423---Greatest Common Increasing Subsequence(最长公共上升子序列)
- Greatest Common Increasing Subsequence(最长单调递增公共子序列+hdu1423)
- hdu1423 Greatest Common Increasing Subsequence(最长递增公共子序列)
- HDU1423 Greatest Common Increasing Subsequence(最长公共递增子序列)
- HDU1423&ZOJ2432 - Greatest Common Increasing Subsequence(LCIS最长公共上升子序列模板)
- 最长公共上升子序列——hdu1423 Greatest Common Increasing Subsequence
- HDU 1423 Greatest Common Increasing Subsequence 最长公共递增序列
- poj 2127 Greatest Common Increasing Subsequence (最长公共递增子序列)
- hdu 1423 Greatest Common Increasing Subsequence(最长公共递增子序列lcis)
- 最长单调递增公共子序列(路径记录+poj2127+zoj2432)Greatest Common Increasing Subsequence
- hdu 1423 Greatest Common Increasing Subsequence(最长公共递增子序列LICS)
- Greatest Common Increasing Subsequence-最长公共上升子序列
- hdu1423Greatest Common Increasing Subsequence(最长公共递增子序列)
- HDU1423:Greatest Common Increasing Subsequence
- HDU1423 Greatest Common Increasing Subsequence
- hdu1423---Greatest Common Increasing Subsequence
- hdu1423 Greatest Common Increasing Subsequence
- CreateFile()函数详细解释
- 各种视频接口知识
- HDU--1000--A + B Problem
- 在Visual Studio命令提示符界面下编译链接OpenSSL自带的示例程序
- extern 是什么意思?
- Greatest Common Increasing Subsequence hdu1423 最长公共递增子序列
- Spring MVC
- ASP.Net随机生成验证码
- abstract 是什么意思?
- 3.cn/qAVVFu
- lucene简介
- internal 修饰符起什么作用?
- Hive常用的SQL命令操作
- HDU--1001--Sum Problem