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. MVC项目发布到IIS遇到的问题

    MVC4 + .NET Framework 4.5 +Windows Server 2008+ IIS7.5 + 4.0集成模式池 ,发布后可能会遇到404.0 或者403.14错误,在web.con ...

  2. C#光盘刻录

    一.背景介绍 最近公司一个老项目需要添加导出数据到光盘的功能.开始对这功能添加有点抵触的.光盘?都啥年代了. 光盘一种即将淘汰的存储媒介.就像当年的随身听,Mp3,Mp4一样,即将退出历史舞台.领导让 ...

  3. Calendar类经常用法 日期间的转换 set方法有巨坑

           今天发现项目的工具类方法有个bug,并且还能迷惑你的bug,刚開始也是非常迷惑,由于这个bug之前出现过,可是过了两天就自己好了.今天又出现了.哦对,今天是 2017年3月31日,之 ...

  4. SQL 将两个结构相同的表合并到成一个表

    select * into 新表名 from (select * from T1 union all select * from T2) 这个语句可以实现将合并的数据追加到一个新表中. 不合并重复数据 ...

  5. 参数数组(params)的用法

    使用参数数组的注意事项: 1. 只能在一维数组上使用params关键字. 2. 不能重载一个只基于params关键字的方法.params关键字不构成方法的签名的一部分. 如: //编译时错误:重复访问 ...

  6. sql自动增长标识(转载)

    sql自动增长标识   对于一个设了自动增长标识的数据表来说,它的字段的值是由数据库自动设置的:这在导数据时很麻烦.   当我们导数据时,我们往往想想将标识字段的数据也导进来,怎么办呢?   方法有两 ...

  7. Unity中差乘判断目标是否在左边或右边

    使用差乘判断左右一般是比较差乘的y,小于0是左,大于0是右.特殊情况可以用其他分量来比较 默认情况: var cross = Vector3.Cross(lhsObject.transform.pos ...

  8. oracle中查看正在运行的并行进程

    select count(*) from v$px_process a where a.STATUS='IN USE';

  9. www--摘录图解TCP/IP

    万维网,www,world wide web,也称web.将互联网中的信息以超文本的形式展现的系统.可以显示www信息的客户端软件叫做web浏览器. www内容 www定义了3个重要的概念,它们分别是 ...

  10. 【shell】tar命令详解

    tar [-cxtzjvfpPN] 文件与目录 ....参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个压缩文件的参数指令!-t :查看 tarfile 里面的文件! ...