最长递增子序列问题

时间:2014-02-20 06:42:22   收藏:0   阅读:320

看了C++语言课程设计的书,有一个最长不减子序列的问题,看的挺有兴趣,看书上的解释的老是看不懂,于是偶不管三七二十一运行试一下,我靠!!!错误的,无语中,只有上网找,又看了N久,真是学艺不精!

看代码:

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
#include <iostream> 
using namespace std; 
#define len(a) (sizeof(a) / sizeof(a[0])) //数组长度 
int lis(int arr[], int len) 
    int longest[len]; 
    for (int i=0; i<len; i++) 
        longest[i] = 1; 
   
    for (int j=1; j<len; j++) { 
        for (int i=0; i<j; i++) { 
            if (arr[j]>arr[i] && longest[j]<longest[i]+1){ //注意longest[j]<longest[i]+1这个条件,不能省略。 
                longest[j] = longest[i] + 1; //计算以arr[j]结尾的序列的最长递增子序列长度 
            
        
    
   
    int max = 0; 
    for (int j=0; j<len; j++) { 
        cout << "longest[" << j << "]=" << longest[j] << endl; 
        if (longest[j] > max) max = longest[j];  //从longest[j]中找出最大值 
    
    return max; 
   
int main() 
    int arr[] = {1, 4, 5, 6, 2, 3, 8}; //测试数组 
    int ret = lis(arr, len(arr)); 
    cout << "max increment substring len=" << ret << endl; 
    return 0; 

  原文地址在这:http://qiemengdao.iteye.com/blog/1660229

有几种解法,虽然原文说不允许转载,但抄袭应该可以吧!

原文:http://www.cnblogs.com/ccccnzb/p/3556533.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!