动态规划--最长上升子序列(Longest increasing subsequence)
前面写了最长公共子序列的问题。然后再加上自身对动态规划的理解,真到简单的DP问题很快就解决了。其实只要理解了动态规划的本质,那么再有针对性的去做这方的题目,思路很快就会有了。不错不错~加油
题目描述:POJ2533
给出一个数列,找出这个数列中最长上升子序列中所包含的个数。
解题思路:
DP问题解题的一般方法就是自下而上,即先求解小的问题,然后再根据小的问题来解决大的问题,最后得到解。但是这里还要满足的条件是最优子结构,即最优解包含着其子问题的最优解。
那么我们首先用arr[]数组(从0下标开始)存储要求的数列,用longest_num[i]数组来记录以i为结尾的子序列里面包含的最长上升子序列的数字个数。然后用循环控制,从下标为1开始求longest_num,并且记录找到的最大值,即可得到解。在我的程序里面,我还加了一个功能就是把最长上升子序列打印出来,如果存在有多个的话,那么就只打印最后一个。
最后根据下面的DP方程就可以进行求解了:
longest_num[i] = max{longest_num[j] + 1,longest_num[i]} 其中j < i && arr[j] < arr[i]
程序:
#include <stdio.h> #define MAX_N 1001 int arr[MAX_N];
int longest_num[MAX_N];
int bt[MAX_N];
int max_point = ; int LIS(int n)
{ int max = ; //最长上升子序列的个数
int i,j; for (i = ; i < n; i++) //i下标之前(包括i)的最长上升子序列的个数
{
longest_num[i] = ;
} for (i = ; i < n; i++) //用于回溯
{
bt[i] = -;
} for (i = ; i < n; i++)
{
for (j = ; j < i; j++)
{
if (arr[i] > arr[j] && longest_num[i] < longest_num[j] + )
{
longest_num[i] = longest_num[j] + ;
if (longest_num[i] >= max)
{
max = longest_num[i];
max_point = i;
bt[i] = j;
}
}
}
} return max;
} void backtrack(int point)
{
if (- == bt[point])
{
printf("%d ",arr[point]);
return;
}
else
{
backtrack(bt[point]);
printf("%d ",arr[point]);
}
} int main()
{
int n,i,ret;
FILE *fp; fp = fopen("in.txt","r");
if (fp == NULL)
{
printf("fopen error!\n");
return -;
} fscanf(fp,"%d",&n);
for (i = ; i < n; i++)
{
fscanf(fp,"%d",&arr[i]);
} ret = LIS(n);
printf("%d\n",ret); backtrack(max_point);
printf("\n"); return ;
} 2013/8/16 16:21
测试数据:
7
1 7 3 5 9 4 8
测试结果:
动态规划--最长上升子序列(Longest increasing subsequence)的更多相关文章
- nlog(n)解动态规划--最长上升子序列(Longest increasing subsequence)
最长上升子序列LIS问题属于动态规划的初级问题,用纯动态规划的方法来求解的时间复杂度是O(n^2).但是如果加上二叉搜索的方法,那么时间复杂度可以降到nlog(n). 具体分析参考:http://b ...
- [Swift]LeetCode300. 最长上升子序列 | Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- 300最长上升子序列 · Longest Increasing Subsequence
[抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...
- 【转】动态规划:最长递增子序列Longest Increasing Subsequence
转自:https://www.cnblogs.com/coffy/p/5878915.html 设f(i)表示L中以ai为末元素的最长递增子序列的长度.则有如下的递推方程: 这个递推方程的意思是,在求 ...
- 最长递增子序列(Longest increasing subsequence)
问题定义: 给定一个长度为N的数组A,找出一个最长的单调递增子序列(不要求连续). 这道题共3种解法. 1. 动态规划 动态规划的核心是状态的定义和状态转移方程.定义lis(i),表示前i个数中以A[ ...
- 算法实践--最长递增子序列(Longest Increasing Subsquence)
什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5 ...
- [Swift]LeetCode594. 最长和谐子序列 | Longest Harmonious Subsequence
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- 最长递增子序列(Longest Increase Subsequence)
问题 给定一个长度为N的数组,找出一个最长的单调自增子序列(不一定连续,但是顺序不能乱).例如:给定一个长度为6的数组A{5, 6, 7, 1, 2, 8},则其最长的单调递增子序列为{5,6,7,8 ...
- 最长公共子序列(Longest common subsequence)
问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子序列.(子序列中的字符不要求连续) 这道题可以 ...
随机推荐
- Android Studio 1.1.0 切换主题和绑定 代码提示 快捷键
这篇文章用于给刚从eclipse 转用 Android Studio 1.1.0的同学看的. 所以经常会更新的. 至于为什么要转Android Studio 1.1.0呢,就自己想吧.没有人强逼的. ...
- [fixed] 解决 slf4j + log4j eclipse 可以打印日志,而在云服务器上不能打印
今天发现服务上没有打印任何日志,而log4j已经设置为了INFO 很奇怪,在eclipse中是可以打印的,也能输出到单独的日志中 后来发现原来是冲突了 把log4j注释掉即可 保留slf4j即可
- Unity GPU Instancing的使用尝试
似乎是在Unity5.4中开始支持GPU Instacing,但如果要比较好的使用推荐用unity5.6版本,因为这几个版本一直在改. 这里测试也是使用unity5.6.2进行测试 在5.6的版本里, ...
- 查看Android内存的8中方法
方法一: 通过手机上Running services的Activity查看,可以通过Setting->Applications->Running services进. 关于Running ...
- android: getDimension, getDimensionPixelOffset 和getDimensionPixelSize 区别
◆结论: getDimension 获取某个dimen的值,如果是dp或sp的单位,将其乘以density,如果是px,则不乘 返回float getDimensionPixelOffset 获取 ...
- [Windows Azure] Getting Started with Windows Azure SQL Data Sync
Getting Started with Windows Azure SQL Data Sync In this tutorial, you learn the fundamentals of Win ...
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- Python实现堆数据结构
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/3/18 19:47 # @Author : baoshan # @Site ...
- 【LDA】线性判别式分析
1. LDA是什么 线性判别式分析(Linear Discriminant Analysis),简称为LDA.也称为Fisher线性判别(Fisher Linear Discriminant,FLD) ...
- CentIOS PHP 扩展库
1.GD库 yum -y install php-gd