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)的更多相关文章

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

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

  2. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

  3. HDU 1423 Greatest Common Increasing Subsequence(LCIS)

    Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...

  4. HDU 1423 Greatest Common Increasing Subsequence

    最长公共上升子序列   LCIS 看这个博客  http://www.cnblogs.com/nuoyan2010/archive/2012/10/17/2728289.html #include&l ...

  5. HDU 1423 Greatest Common Increasing Subsequence ——动态规划

    好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...

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

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

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

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

  8. 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 ...

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

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

随机推荐

  1. Linux 备份工具

     Linux 备份工具 GNU 的传统备份工具  GNU tar — http://www.gnu.org/software/tar/ GNU cpio — http://www.gnu.org/so ...

  2. hdu 5533 Dancing Stars on Me(数学,水)

    Problem Description The sky was brushed clean by the wind and the stars were cold in a black sky. Wh ...

  3. UI设计师不可不知的安卓屏幕知识

    不少设计师和工程师都被安卓设备纷繁的屏幕搞得晕头转向,我既做UI设计,也做过一点安卓界面布局,刚好对这块内容比较熟悉,也曾在公司内部做过相关的讲座,在此,我将此部分知识重新梳理出来分享给大家! 1.了 ...

  4. 【枚举+贪心】【TOJ3981】【ICPC Balloons】

    给你N种不同颜色气球,每种气球有个数目 count[i],给的同种颜色气球可能是L尺寸,或M尺寸. M个问题,每个问题有个解决人数ac[i]. 每个问题 要分配一种颜色的气球,尺寸要一样 现在 这些气 ...

  5. springmvc 传递和接收数组参数

    java url中如何传递数组,springMVC框架controller类如何接收数组参数? 下面介绍一下URL中传递数组参数方法: dd.do?titles[]=col1&titles[] ...

  6. Cassandra - Non-system keyspaces don't have the same replication settings, effective ownership information is meaningless

    In cassandra 2.1.4, if you run "nodetool status" without any keyspace specified, you will ...

  7. css中关于transform、transition、animate的区别

    写动画经常会用到这几个属性,他们之间有什么区别呢? 1.transform 每每演示transform属性的,看起来好像都是带动画.这使得小部分直觉化思维的人(包括我)认为transform属性是动画 ...

  8. 键盘事件之keydown keypress keyup区别

    经过测试,显然事件执行的顺序是: keydown->keypress->keyup. 但是连续按一个按键的话,会一直触发:keydown keypress.直到你提起按键,会触发keyup ...

  9. Equal 和==比较

    Equal 和==比较 ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同,要比较两个基本类型的数据或两个引用变量是否相当,只能用==操作符. 如果一个变 ...

  10. 7.19 SQL——函数

    select * from student select * from score select * from teacher select * from course select * from c ...