求最大子序列和

比较经典的算法问题,能够很好的体现动态规划的实现,以一点“画龙点睛” 大大精简了算法复杂度,且实现简单。本文中实现了4种:

一般 maxSubSequenceSum0 O(n^3)

简单优化过的算法 maxSubSequenceSum1 O(n^2)

分治法优化的算法 maxSubSequenceSum2 O(n*log(n))

动态规划的算法 maxSubSequenceSum3 O(n)

#include <math.h>

#include "mymath.h"

/*
* 计算序列的某段子序列的和,maxSubSequenceSum0使用
*/
static int subSequenceSum(int a[], int left, int right)
{
    int i, sum = 0;
    for (i = left; i <= right; i++)
    {
        sum = sum + a[i];
    }
    return sum;
}

/*
* 三层遍历求子序列和的最大值,算法复杂度O(n^3)
*/
int maxSubSequenceSum0(int a[], int len)
{
    int i, j;
    int curSum; /* 当前序列和 */
    int maxSum; /* 最大序列和 */

    /* 初始化最大子序列和为序列第一个元素 */
    maxSum = a[0];

    /* 第一层循环定义子序列起始位置 */
    for (i = 0; i < len; i++)
    {
        /* 起始位置为i,初始化当前和为0 */
        curSum = 0;

        /* 第二层循环定义子序列结束位置 */
        for (j = i; j < len; j++)
        {
            /* 第三层循环在函数sumSubseqence中,计算子序列和 */
            curSum = subSequenceSum(a, i, j);

            /* 与最大子序列和比较,更新最大子序列和 */
            if (curSum > maxSum)
            {
                maxSum = curSum;
            }
        }
    }
    return maxSum;
}

/*
* 双层遍历求子序列和的最大值,算法复杂度O(n^2)
*/
int maxSubSequenceSum1(int a[], int len)
{
    int i, j;
    int curSum; /* 当前序列和 */
    int maxSum; /* 最大序列和 */

    /* 初始化最大子序列和为序列第一个元素 */
    maxSum = a[0];

    /* 外层循环定义子序列起始位置 */
    for (i = 0; i < len; i++)
    {
        /* 起始位置为i,初始化当前和为0 */
        curSum = 0;

        /* 内层循环定义子序列结束位置 */
        for (j = i; j < len; j++)
        {
            /* 计算子序列和,并与最大子序列和比较,更新最大子序列和 */
            curSum = curSum + a[j];

            /* 与最大子序列和比较,更新最大子序列和 */
            if (curSum > maxSum)
            {
                maxSum = curSum;
            }
        }
    }
    return maxSum;
}

/*
* 某段字序列中,含左边界元素的字序列和中的最大值,_maxSubSequenceSum2中使用
*/
static int _maxLeftBoderSubSequenceSum(int a[], int left, int right)
{
    int i;
    int sum = 0;
    int maxSum = a[left];
    for (i = left; i <= right; i++)
    {
        sum += a[i];
        if (sum > maxSum)
        {
            maxSum = sum;
        }
    }
    return maxSum;
}

/*
* 某段字序列中,含右边界元素的字序列和中的最大值,_maxSubSequenceSum2中使用
*/
static int _maxRightBoderSubSequenceSum(int a[], int left, int right)
{
    int i;
    int sum = 0;
    int maxSum = a[right];
    for (i = right; i >= left; i--)
    {
        sum += a[i];
        if (sum > maxSum)
        {
            maxSum = sum;
        }
    }
    return maxSum;
}

/*
* 求序列某段子序列中子序列和最大值
*/
static int _maxSubSequenceSum2(int a[], int left, int right)
{
    int center;
    int leftMaxSum;
    int rightMaxSum;
    int maxLeftBorderSum;
    int maxRightBorderSum;

    /* 递归终止条件 */
    if (left == right)
    {
        return a[left];
    }

    /* 分治法递归开始,取中点二分处理 */
    center = (left + right) >> 1; /* center = (left + right) / 2; */

    /* 递归求左右子序列段中最大子序列和 */
    leftMaxSum = _maxSubSequenceSum2(a, left, center);
    rightMaxSum = _maxSubSequenceSum2(a, center + 1, right);

    maxLeftBorderSum = _maxRightBoderSubSequenceSum(a, left, center);
    maxRightBorderSum = _maxLeftBoderSubSequenceSum(a, center + 1, right);

    /*
     * 二分后的最大值有三个:
     *    1、leftMaxSum,左段最大子序列和
     *    2、rightMaxSum,右段最大子序列和
     *    3、maxLeftBorderSum+maxRightBorderSum,左段最大含右边界子序列和最大值和右段最大含左边界子序列和最大值,二者之和
     * 这三者中的最大值即为分段前的最大子序列和
     * 
     * 分治算法核心部分,解决分治后结果归并问题,具体分析:
     *    这是对分段后的子序列的一种划分,有三种,只需分别求出各种的最大值然后在三者之间取一个最大值即可:
     *       1、子序列全在左段,最大子序列和为leftMaxSum
     *       2、子序列全在右段,最大子序列和为rightMaxSum
     *       3、子序列跨左右段,最大字序列和为maxLeftBorderSum+maxRightBorderSum
     */
    return tmax(leftMaxSum, rightMaxSum, maxLeftBorderSum+maxRightBorderSum);
}

/*
* 分治法实现,算法复杂度O(n*log(n))
* 分:使用二分法进行分段
* 治:详细算法见_maxSubSequenceSum2内描述,简述为:
*    全段最大子序列为以下三者中的最大值
*       左段最大子序列和
*       右段最大子序列和
*       左段最大含右边界子序列和最大值和右段最大含左边界子序列和最大值之和
*/
int maxSubSequenceSum2(int a[], int len)
{
    return _maxSubSequenceSum2(a, 0, len - 1);
}

/*
* 动态规划实现,算法复杂度O(n)
*/
int maxSubSequenceSum3(int a[], int len)
{
    int i;
    int curSum; /* 当前序列和 */
    int maxSum; /* 最大序列和 */

    /* 初始化当前序列和为0 */
    curSum = 0;

    /* 初始化最大子序列和为序列第一个元素 */
    maxSum = a[0];

    /* 开始循环求子序列和 */
    for (i = 0; i < len; i++)
    {
        curSum = curSum + a[i];

        /* 与最大子序列和比较,更新最大子序列和 */
        if (curSum > maxSum)
        {
            maxSum = curSum;
        }

        /* 动态规划部分,舍弃当前和为负的子序列 */
        if (curSum < 0)
        {
            curSum = 0;
        }
    }
    return maxSum;
}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

下面是自己的一些总结:

对于第一种方法,可以返回相应的序列的开始序号和结束的序号;

#include <cmath>

#include <iostream>

using namespace std;

 

//########################################

int SubSequenceSum(int a[],int left,int right)

{

         int i, sum = 0;

         for(i=left;i<=right;i++)

         {

                   sum += a[i];

         }

         return sum;

}

 

int MaxSubSequenceSum(int a[],int len,int &start,int &end)

{

         int i, j;

         int curSum;   //当前子序列的和

         int maxSum; //最大子序列的和

         //int start,end;

        

         maxSum = a[0]; 

         start = end = 0;

        

         for(i=0;i<len;i++)

         {

                   curSum = 0;

                   for(j=i;j<len;j++)

                            curSum = SubSequenceSum(a,i,j);

                   if(curSum>maxSum)

                   {

                            maxSum = curSum;

                            start = i;

                            end = j;

                   }

         }

         return maxSum;

}

 

//@@@@@@@@@@@@@@@@@@@@@@@@@@@

int MaxSubSequenceSum1(int a[],int len,int &start,int &end)

{

         int i,j;

         int curSum;

         int maxSum;

 

         maxSum = a[0];

 

         for(i = 0;i<len;i++)

         {

                   curSum = 0;

                   for(j=i;j<len;j++)

                   {

                            curSum += a[j];

                            if(curSum>maxSum)

                            {

                                     maxSum = curSum;

                            }

                   }

         }

         return maxSum;

}

 

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

int _MaxLeftBoderSubSequenceSum(int a[],int left,int right)

{

         int i;

         int sum = 0;

         int maxSum = a[left];

         for(i=left;i<=right;i++)

         {

                   sum += a[i];

                   if(sum>maxSum)

                   {

                            maxSum = sum;

                   }

         }

         return maxSum;

}

 

int _MaxRightBoderSubSequenceSum(int a[],int left,int right)

{

         int i;

         int sum = 0;

         int maxSum = a[left];

         for(i=right;i>=left;i--)

         {

                   sum += a[i];

                   if(sum>maxSum)

                   {

                            maxSum = sum;

                   }

         }

         return maxSum;

}

 

int MaxSubSequenceSum3(int a[],int len)

{

         int i;

         int curSum;

         int maxSum;

         int start,end;

 

         start = end = 0;

         curSum = 0;

         maxSum = a[0];

 

         for(i=0;i<len;i++)

         {

                   curSum += a[i];

                   if(curSum>maxSum)

                   {

                            maxSum = curSum;

                            end = i;

                   }

                   //当子序列的和为负数时则清零

                   if(curSum<0)

                   {

                            curSum = 0;

                            start = i + 1;

                   }                

         }

         cout<<" "<<start<<","<<end<<endl;

         return maxSum;

}

 

int main ()

{

         int a[] = {1,2,-5,9,-4,11,-5,6,-7,8};

         int start,end;

         int max = MaxSubSequenceSum(a,sizeof(a)/sizeof(int),start,end);

         cout<<max<<":"<<start<<" "<<end<<endl;

         int max1 = MaxSubSequenceSum1(a,sizeof(a)/sizeof(int),start,end);

         cout<<max1<<":"<<start<<" "<<end<<endl;

         int max3 = MaxSubSequenceSum1(a,sizeof(a)/sizeof(int),start,end);

         cout<<max1<<":"<<start<<" "<<end<<endl;

         return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值