[编程之美]求数组中最长递增子序列
来源:互联网 发布:windows10 优化设置 编辑:程序博客网 时间:2024/05/05 17:16
比如下列数组
最长递增子序列为1, 2, 4, 6,当我们考虑到第i个元素时,当前最大的递增子序列是完全由前一个元素的最大递增子序列确定的,而不管前面的排列是怎样,因此,这个问题满足无后效性,可以用动态规划来实现
LIS[i+1] = max{1, LIS[k] + 1}, array[i+1] > a[k] , k从0到i
#include <iostream>using namespace std;int main() { int a[8] = {1, -4, 3, -3, -2, -1, 0, 4}; int LIS[8] = {}; int f[8] = {}; int max_index = 0; int size, s; for (int i = 0; i < 8; i++) { LIS[i] = 1; for (int j = 0; j < i; j++) { if (LIS[i] < LIS[j] + 1 && a[i] > a[j]) LIS[i] = LIS[j] + 1; } if (LIS[i] > LIS[max_index]) max_index = i; } size = s = LIS[max_index]; f[--size] = a[max_index]; for (int j = max_index-1; j >= 0; j--) { if (LIS[j]+1 == LIS[max_index]) { f[--size] = a[j]; max_index = j; } } for (int i = 0; i < s; i++) cout << f[i] << " "; cout << endl; return 0;}
- [编程之美]求数组中最长递增子序列
- 编程之美--求数组中最长递增子序列
- 编程之美--求数组中最长递增子序列
- 编程之美 求数组中的最长递增子序列
- 读书笔记之编程之美 - 2.16 求数组中最长递增子序列
- 编程之美读书笔记之2.16求数组中最长递增子序列
- 编程之美: 第二章 数字之魅 2.16求数组中最长递增子序列
- 编程之美读书笔记_2.16 求数组中最长递增子序列
- 《编程之美》读书笔记17: 2.16 求数组中最长递增子序列
- 编程之美2.16 求数组中最长递增子序列
- 编程之美2.16——求数组中最长递增子序列
- 编程之美2.16求数组中最长递增子序列
- 编程之美_012求数组中最长递增子序列
- 求数组中最长递增子序列—动态规划入门(编程之美)
- POJ 2533 Longest Ordered Subsequence 编程之美 2.16 求数组中最长递增子序列
- 编程之美---求数组中最长递增子序列LIS
- 编程之美 2.16 求数组中最长递增子序列
- 【编程之美】2.16 求数组中最长递增子序列
- QtCreator中DLL的创建和使用(权威指南,经验证):基类非QObject的类
- linux守护进程
- 字符类型和字符串类型相互转换
- 用WebView或手机浏览器打开连接问题
- FBReaderJ流程
- [编程之美]求数组中最长递增子序列
- google map 自定义infowindow
- html分割线
- 程序调试信息的输出__FILE__,__LINE__,不定参数的宏定义
- linux静态库的生成与使用
- OuterInnerClassSon 内部类定义子类
- iPhone开发之 ARC是什么
- InstallShield安装完成后运行第三方软件
- 比较表变量和临时表(很不错)