我是靠谱客的博主 健壮棉花糖,这篇文章主要介绍最长上升子序列问题(LIS),现在分享给大家,希望可以做个参考。

问题

这里写图片描述

方法

  • 状态转移方程:
    状态转移方程

  • 需要注意的是: 当完全逆序的情况下,每个 dp(i) = 1

代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream> using namespace std; int data[] = {1,6,2,3,7,5}; int record[6]; int max(int a, int b) { return a > b? a:b; } int main() { int len = sizeof(data) / sizeof(int); int ans = 0; //用于记录最长的子串长度 for (int i = 0; i < len; i++) record[i] = 1; //初始化 for (int i = 0; i < len; i++) { for (int j = 0; j < i; j++) { if (data[j] < data[i]) { record[i] = max(record[i], record[j] + 1); } if (record[i] > ans) { ans = record[i]; } } } //这里打印路径的程序写的很巧妙 cout << ans; int num = ans; int ls[num]; for (int j = len-1; j >=0; j--) { if (num == record[j]) { ls[--num] = j; } } for (int i = 0; i < ans; i++) { cout << ls[i] << " "; } }
  • 时间复杂度: O(n2)

参考

  • http://blog.csdn.net/q547550831/article/details/51920052
  • -

最后

以上就是健壮棉花糖最近收集整理的关于最长上升子序列问题(LIS)的全部内容,更多相关最长上升子序列问题(LIS)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部