首页 > 编程语言 > 详细

回顾经典问题算法:LIS, LCS-(DP类别)

时间:2015-03-02 18:40:00      阅读:289      评论:0      收藏:0      [点我收藏+]

LIS,最长递增子序列

 1 #include <iostream>
 2 #include <cstdlib>
 3 
 4 using namespace std;
 5 
 6 int LIS(int* arr, int len) {
 7     if (arr == NULL || len < 1) return 0;
 8     int LIS[len] = {0};
 9     int mlen = 0;
10     for (int i=0; i<len; i++) {
11         LIS[i] = 1;
12         for (int j = 0; j < i; j++) {
13             if (arr[j] < arr[i] && LIS[i] < LIS[j] + 1) {
14                 LIS[i] = LIS[j] + 1;
15             }
16         }
17         if (LIS[i] > mlen) {
18             mlen = LIS[i];
19         }
20     }
21     return mlen;
22 }
23 
24 
25 int main() {
26     int arr[] = {1, -1, 2, -3, 4, -5, 6, -7};
27     int len = sizeof(arr) / sizeof(arr[0]);
28     cout<<LIS(arr, len)<<endl;
29     system("pause");
30     return 0;
31 }

 

 

LCS,最长公共子序列

回顾经典问题算法:LIS, LCS-(DP类别)

原文:http://www.cnblogs.com/lailailai/p/4309394.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!