HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
http://acm.hdu.edu.cn/showproblem.php?pid=1423
题意:
给你两个数字组成的串a和b,要你求出它们的最长公共严格递增子序列的长度(LCIS).
分析:
首先我们令f[i][j]==x表示是a串的前i个字符与b串的前j个字符构成的且以b[j]结尾的LCIS长度.
当a[i]!=b[j]时:
f[i][j]=f[i-1][j]
当a[i]==b[j]时:
f[i][j]=max(f[i-1][k])+1. 当中 k<j且b[k]<b[j].
假设我们按上述递推公式依次枚举i, j, k 的话,那么时间复杂度就是O(n*m^2)了.
事实上我们仅仅要枚举i, j. 然后我们记录当前的最大f[i-1][k]值就可以(要满足k<j且b[k]<b[j]). 程序实现用到了一个技巧, 即枚举(i,
j)情况时如果a[i]的值与b[j+1]的值是相等的. 那么仅仅要b[j]<a[i]的话, 我们直接更新max=f[i-1][j]就可以. 如果下一轮a[i]==b[j+1], 那么上一轮max保存的值f[i-1][j] 能够肯定j<j+1 且b[j]<a[i]==b[j+1]. (当b[j]变成b[k]也是一样)
怎样输出一个LCIS串呢?
我们首先找到使得f[n][id]取最大值的id. 然后它肯定是由f[n-1][k](k<id且b[k]<b[id]) 构成的. 所以我们仅仅须要往前找到那个f[n-1][k]==f[n][id]-1 且 b[k]<b[id]的值逆序输出就可以.
事实上动态规划的全部输出方案的问题都能够这么输出.
假设想输出字典序最小的LCIS串呢?
我们仅仅须要将原来的两个序列逆转,然后找出最长公共递减子序列. 然后从第一个LCDS的字符開始找尽可能字典序小的字符就可以. 事实上思想大致都是一样的.
AC代码:
</pre><pre name="code" class="cpp">#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=500+5; int n;//a串长
int m;//b串长
int a[maxn];//a串
int b[maxn];//b串
int f[maxn][maxn]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d",&b[i]); memset(f,0,sizeof(f));
for(int i=1;i<=n;i++)
{
int max=0;//当前f[i-1][k]最大值且 k<j&&b[k]<b[j]
int flag=-1;
for(int j=1;j<=m;j++)
{
f[i][j]=f[i-1][j];
if(a[i]>b[j] && max<f[i-1][j])
{
max=f[i-1][j];
flag=j;
}
if(a[i]==b[j])
{
f[i][j]=max+1;
}
}
} int max_val=0;
int id=-1;
for(int i=1;i<=m;i++)
{
if(max_val<f[n][i])
{
max_val=f[n][i];
id=i;
}
} printf("%d\n",max_val);
if(T) printf("\n"); //逆序输出一个LCIS串
/*
int i=n;
while(id!=-1 && f[i][id]>=1)
{
printf("%d ",b[id]);
int tmp=f[i][id];
int tmp_v=b[id];
//往前找到合法的f[i-1][k]
while(id!=0)
{
id--;
if(f[i-1][id]==tmp-1 && b[id]<tmp_v)
break;
}
i--;
}
printf("\n");
*/ }
return 0;
}
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)的更多相关文章
- HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- HDU 1423 Greatest Common Increasing Subsequence(LCIS)
Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...
- HDU 1423 Greatest Common Increasing Subsequence
最长公共上升子序列 LCIS 看这个博客 http://www.cnblogs.com/nuoyan2010/archive/2012/10/17/2728289.html #include&l ...
- HDU 1423 Greatest Common Increasing Subsequence ——动态规划
好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- Unix/Linux环境C编程入门教程(32) 环境变量那些事儿
1. getenv() putenv()setenv()函数介绍 getenv(取得环境变量内容) 相关函数 putenv,setenv,unsetenv 表头文件 #include<stdli ...
- tftp使用方法
参数说明:-l 是local的缩写,后跟存在于Client的源文件名,或下载Client后 重命名的文件名. -r 是remote的缩写,后跟Se ...
- html类,id规范命名
DIV+CSS的命名规则 SEO(搜索引擎优化)有很多工作要做,其中对代码的优化是一个很关键的步骤.为了更加符合SEO的规范,下面中部IT网将对目前流行的CSS+DIV的命名规则整理如下: 页头:he ...
- linux命令之mount
熟悉linux的同学都应该知道mount命令.在linux中,一切皆文件.硬盘分区都是以文件目录的方式存在. 如果我们想访问移动硬盘,U盘等我们必须将这些设备mount到我们linux文件系统中某个目 ...
- HDU 1796 Howmany integers can you find (容斥原理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Android学习总结——Activity状态保存和恢复
Android中启动一个Activity如果点击Home键该Activity是不会被销毁的,但是当进行某些操作时某些数据就会丢失,如下: Java class: package com.king.ac ...
- poj 3255 求次大最短路
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5508 Accepted: 2088 Descri ...
- HTTP请求的基本概念 HTTP请求头和响应头的含义
1,HTTP请求的基本概念 TCP/UPD/HTTP *2,HTTP请求头和响应头的含义 请求头: Accept: text/html,image/*(浏览器可以接收的类型) Acc ...
- UVA 10603 Fill
题意: 题目的意思是倒水,给出的四个数据是第一个水杯,第二个水杯,第三个水杯,和目标水量.一开始只有第三个水杯是满的,剩下的水杯是空的.倒水的时候只能把倒水出来的这个杯子倒空,或是倒水进去的杯子倒满. ...
- JavaScript之cookie
JavaScript通过Cookie实现简单的用户登录状态的保存.Cookie可以跨越多个网页使用,但不能跨域名使用,也不同跨浏览器使用. 1.设置cookie function SetCookie( ...