The Longest Increasing Subsequence (LIS) problem requires us to find a subsequence t of a given sequence s, such that t satisfies two requirements:

  1. Elements in t are sorted in ascending order;
  2. t is as long as possible.

This problem can be solved using Dynamic Programming. We define the state P[i] to be the length of the longest increasing subsequence ends at i (with s[i] as its last element). Then the state equations are:

  1. P[i] = max_{j = 0, ..., i - 1 and arr[j] < arr[i]} P[j] + 1;
  2. If no such j exists, P[i] = 1.

Putting these into code using a table to store results for smaller problems and solve it in a bottom-up manner. We will have the following code.

 #include <iostream>
#include <string>
#include <vector> using namespace std; int longestIncreasingSubsequence(vector<int>& nums) {
vector<int> dp(nums.size(), );
int maxlen = ;
for (int i = ; i < nums.size(); i++) {
for (int j = ; j < i; j++) {
if (nums[j] < nums[i] && dp[j] + > dp[i]) {
dp[i] = dp[j] + ;
maxlen = max(maxlen, dp[i]);
}
}
}
return maxlen;
} void longestIncreasingSubsequenceTest(void) {
int num[] = {, , , , , , , , };
vector<int> nums(num, num + sizeof(num) / sizeof(int));
printf("%d\n", longestIncreasingSubsequence(nums));
} int main(void) {
longestIncreasingSubsequenceTest();
system("pause");
return ;
}

This program only computes the length of the LIS. If you want to print all the possible LIS, you need to modify the above program. Specifically, you may want to use backtracking to obtain all the possible LIS. My code is as follows. Welcome for any comments. Thank you!

 #include <iostream>
#include <string>
#include <vector> using namespace std; /* Helper function to find all LCS. */
void findAllLCSHelper(vector<int>& nums, vector<int>& dp, vector<int>& seq, vector<vector<int> >& res, int maxlen, int end) {
if (maxlen == ) {
reverse(seq.begin(), seq.end());
res.push_back(seq);
reverse(seq.begin(), seq.end());
return;
}
for (int i = end; i >= ; i--) {
if (dp[i] == maxlen && (seq.empty() || nums[i] < seq.back())) {
seq.push_back(nums[i]);
findAllLCSHelper(nums, dp, seq, res, maxlen - , i - );
seq.pop_back();
}
}
} /* Function to find all LCS. */
vector<vector<int> > findAllLCS(vector<int>& nums, vector<int>& dp, int maxlen) {
vector<vector<int> > res;
vector<int> seq;
findAllLCSHelper(nums, dp, seq, res, maxlen, nums.size() - );
return res;
} /* Compute the length of LCS and print all of them. */
int longestIncreasingSubsequence(vector<int>& nums) {
vector<int> dp(nums.size(), );
int maxlen = ;
for (int i = ; i < (int)nums.size(); i++) {
for (int j = ; j < i; j++) {
if (nums[j] < nums[i] && dp[j] + > dp[i]) {
dp[i] = dp[j] + ;
maxlen = max(maxlen, dp[i]);
}
}
}
vector<vector<int> > lcss = findAllLCS(nums, dp, maxlen);
for (int i = ; i < (int)lcss.size(); i++) {
for (int j = ; j < (int)lcss[i].size(); j++)
printf("%d ", lcss[i][j]);
printf("\n");
}
return maxlen;
} /* Test function. */
void longestIncreasingSubsequenceTest(void) {
int num[] = {, , , , , , , , , , , , , , , };
vector<int> nums(num, num + sizeof(num) / sizeof(int));
printf("%d\n", longestIncreasingSubsequence(nums));
} int main(void) {
longestIncreasingSubsequenceTest();
system("pause");
return ;
}

Running this program in Microsoft Visual Professional 2012 gives the following results.


The first four rows are the four LIS.

[Algorithms] Longest Increasing Subsequence的更多相关文章

  1. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  2. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  3. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  4. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  5. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  6. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

  7. 300. Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...

  8. SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

    Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...

  9. leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)

    https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...

随机推荐

  1. C语言之指针基础概念

    今天就写一下关于C语言指针的一些感想吧. 很多同学都搞不懂指针,我一开始也云里雾里没看懂指针,而且老师又把指针说得很难的样子.其实主要是把指针”*“的作用给弄混了,不用畏惧,细心点看就可以了. 首先简 ...

  2. 使用vs远程调试iis站点

    在vs安装目录下IDE文件夹下的Remote Debugger 复制到服务器运行 启动msvsmon.exe msvsmon.exe启动后设置远程连接不验证身份 vs中 调试→附加到进程 ip+端口访 ...

  3. NFS介绍

    一.NFS服务介绍 NFS是 Network File system的缩写 NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机 ...

  4. cisco asa5510 配置

    anyconnect 查看vpn链接 ASA版本8.4(7)    anyconnect版本3.1  亲测sh vpn-sessiondb anyconnect  查看登录用户详情sh vpn-ses ...

  5. zookeeper(一):功能和原理

    简介 ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现.分布式应用程序可以基于 ZooKeeper 实现诸如数据发布/订阅.负载均衡.命名服务.分 ...

  6. 品茗论道说广播(Broadcast内部机制讲解)(下)

    下面我们来看,递送广播动作中最重要的processNextBroadcast(). 3.2 最重要的processNextBroadcast() 从processNextBroadcast()的代码, ...

  7. 李洪强iOS开发之iOS工具收集

    李洪强iOS开发之iOS工具收集 项目 简述 日期 我是怎么慢慢变懒的 : Jenkins + 蒲公英 使用Jenkins + 蒲公英使得项目打包给测试人员自动化,大大节省了劳动力 2015.04.1 ...

  8. layui当点击增加的时候,将form中的值获取的添加到table行中代码

    layui.use(['table','layer'],function(){ var $=layui.$, table=layui.table, layer=layui.layer; functio ...

  9. CefSharp 集成谷歌浏览器详解---(一)环境搭建(没测试过,不知道好不好用)

    https://blog.csdn.net/zpyxman/article/details/78538808

  10. Oracle之比较NVARCHAR2字符串

    一.引言 昨天遇到一个问题,一直想不通是为什么,我在pl/sql里面执行sql语句,选出字段值等于某个值的的行: '; 用select * from test;是可以查到该值的,但是用这条语句却没有结 ...