Longest Increasing Subsequences(最长递增子序列)的两种DP实现
/**
@description: Longest Increasing Subsequence
@author: seiyagoo
@create: 2013.10.25
@modified: 2013.10.26
**/
int LIS_1(int A[], int size){ int *LIS = new int[size];
vector<int> *vec = new vector<int>[size]; /* Compute optimized LIS values in bottom up manner */
for(int i=; i < size; i++){
LIS[i]=; //初始化默认长度
int max_j=, flag=;
for(int j=; j < i; j++){ //查表,找出前面最长的序列, 若将A[i]加入LIS[j](LIS[j]+1的含义)的递增子序列比当前的LIS[i]更长, 则更新LIS[i]
if(A[i] > A[j] && LIS[i] < LIS[j]+){
LIS[i] = LIS[j]+;
max_j=j;
flag=;
}
}
if(flag) //copy前面最长子序列到vec[i]
vec[i].insert(vec[i].end(), vec[max_j].begin(), vec[max_j].end());
vec[i].push_back(A[i]); //最后放入A[i]
} /*Show LIS of the current state*/
vector<int>::iterator it;
cout<<left;
for(int i=; i<size; i++){
cout<<setw()<<A[i]<< " --> ";
for(it = vec[i].begin(); it!=vec[i].end(); it++)
cout<<*it<<" ";
cout<<endl;
} /* Pick maximum of all LIS values, namely max{LIS[i]} */
int max_len=;
for(int i = ; i < size; i++ )
if( max_len < LIS[i] )
max_len = LIS[i]; delete[] LIS;
delete[] vec; return max_len;
}
/**
@description: Longest Increasing Subsequence
@author: seiyagoo
@create: 2013.10.25
@modified: 2013.10.26
**/ // Binary search (note boundaries in the caller)
// A[] is ceilIndex in the caller
int CeilIndex(int A[], int l, int r, int key) {
int m; while( r - l > ) {
m = l + (r - l)/;
(A[m] >= key ? r : l) = m; // ternary expression returns an l-value
} return r;
} int LIS_2(int A[], int size) {
// boundary case: when array size is one
if( == size ) return ; int *tailTable = new int[size];
vector<int> *vec = new vector<int>[size];
int len; // always points empty slot //memset(tailTable, INT_MAX, sizeof(tailTable[0])*size); @bug for(int i = ; i < size; i++)
tailTable[i] = INT_MAX; tailTable[] = A[]; //tailTable[0] store the smallest value
vec[].push_back(A[]); len = ;
for( int i = ; i < size; i++ ) {
if( A[i] < tailTable[] ) { //case 1: new smallest value
tailTable[] = A[i]; /*discard and create*/
vec[].clear();
vec[].push_back(A[i]);
}
else if( A[i] > tailTable[len-] ) { //case 2: A[i] wants to extend largest subsequence
tailTable[len++] = A[i]; /*clone and extend*/
vec[len-] = vec[len-];
vec[len-].push_back(A[i]);
}
else { //case 3: A[i] wants to be current end candidate of an existing subsequence, It will replace ceil value in tailTable
int ceilIndex = CeilIndex(tailTable, -, len-, A[i]);
tailTable[ceilIndex] = A[i]; /*discard, clone and extend*/
vec[ceilIndex].clear();
vec[ceilIndex] = vec[ceilIndex-];
vec[ceilIndex].push_back(A[i]);
} /*Printf all the active lists*/
vector<int>::iterator it;
cout<<left;
cout<<"A["<<i<<"] = "<<A[i]<<endl<<endl;
cout<<"active lists:"<<endl;
for(int i=; i<len; i++){
for(it = vec[i].begin(); it!=vec[i].end(); it++)
cout<<*it<<" ";
cout<<endl;
} /*Printf end elements of all the active lists*/
cout<<endl<<"end elements array:"<<endl;
for(int i = ; i < size; i++)
if(tailTable[i] != INT_MAX)
cout<<tailTable[i]<<" ";
cout<<endl;
cout<<"-------------------------"<<endl;
} delete[] tailTable;
delete[] vec; return len;
}
五、运行结果
example:





Longest Increasing Subsequences(最长递增子序列)的两种DP实现的更多相关文章
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]
题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
随机推荐
- 03003_MySQL
1.之前已经进行过MySQL的安装,参考03002_MySql数据库的安装和配置 : 2.登录MySQL数据库 (1)启动mysql服务:net start mysql : (2)MySQL是一个 ...
- PHP GD 生成图片验证码+session获取储存验证码
以下分享一个PHP制作的图片验证码案例:案比例如以下图: 运用PHP GD具体请看:http://www.php.net/manual/zh/book.image.php 后台图片代码例如以下: &l ...
- HDU 1533 Going Home(KM完美匹配)
HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...
- javascript 获取HTML DOM父,子,临近节点
在Web应用程序特别是Web2.0程序开发中.常常要获取页面中某个元素,然后更新该元素的样式.内容等.怎样获取要更新的元素,是首先要解决的问题.令人欣慰的是,使用JavaScript获取节点的方法有非 ...
- git 工具的使用总结(6)-提交合并处理
1.撤消修改 1)revert:反转提交,它就是把你的一个提交先撤消掉,但是,它跟reset不同的是,你的这次这小会留下记录,这样在你下次需要的时候,可以通过这个节点把撤消的提交恢复 zhangshu ...
- scroolspy滚动监听插件
<nav id="nav" class="navbar navbar-default"> <a href="#" clas ...
- Win8.1恢复这台电脑里的6个文件夹
平台:win8.1 问题:网络下载的ghost版8.1,装好后“这台电脑”里没有6个常用的文件夹. 解决:导入下列注册表项即可 Windows Registry Editor Version 5.00 ...
- PYTHON学习第五天课后总结:
今日重点: 数字类型 字符串类型 列表类型 元组类型 1,数字类型 数字类型为不可变类型 也只能将纯数字类型的字符串转换成int包括: 整型: int() int() 为内置函数,可 ...
- 解决XCODE配置LLVM环境出现的问题
问题描写叙述:在LLVM整合进XCODE的过程中出现符号没有定义导致出现未决函数或方法.但使用终端编译链接生成程序没有问题. 问题产生原因:未引用响应的LLVM库与系统库,以及编译器设置.连接器设置不 ...
- Android判断App是否在前台运行
版权声明:本文为博主原创文章,未经博主允许不得转载. //当前应用是否处于前台 private boolean isForeground(Context context) { if (context ...