Algorithm - 最长递增子序列

来源:互联网 发布:前端ui设计软件 编辑:程序博客网 时间:2024/05/12 20:47

1. 求最长递增子序列(LIS):

int LIS(int * p, int length){    /* 参数合法性检查 */    if (NULL == p || length <= 0)    {        return 0;    }    /* 求最长递增子序列 */    int * pLIS = new int[length];    int i, j;    for (i = 0; i < length; i++)    {        /* 初始化默认长度为1 */        pLIS[i] = 1;        /* 递推计算最长递增子序列 */        for (j = 0; j < i; j++)        {            /* 位置为i的元素是否可以接到之前的最长递增子序列构成更长的递增子序列 */            if (p[i] > p[j] && pLIS[j] + 1 > pLIS[i])            {                /* 更新pLIS数组中对应于i元素的最长递增子序列长度值 */                pLIS[i] = pLIS[j] + 1;            } // if        } // for    } // for    /* 返回最长的长度 */    int max = 1;    for (i = 0; i < length; i++)    {        if (pLIS[i] > max)        {            max = pLIS[i];        }    }    return max;} // end