#include <iostream>
#include <vector>
using namespace std;
int max3( int a, int b, int c )
{
return a > b ? a > c ? a : c : b > c ? b : c;
}
/** 分治法求最大子序列
* Recursive maximum contiguous subsequence sum algorithm.
* Finds maximum sum in subarray spanning a[left..right].
* Does not attempt to maintain actual best sequence.
*/
int maxSumRec( const vector<int> & a, int left, int right )
{
if ( left == right ) // Base case
if ( a[ left ] > 0 )
return a[ left ];
else
return 0;
int center = ( left + right ) / 2;
int maxLeftSum = maxSumRec( a, left, center );
int maxRightSum = maxSumRec( a, center + 1, right );
int maxLeftBorderSum = 0, leftBorderSum = 0;
for ( int i = center; i >= left; i-- )
{
leftBorderSum += a[ i ];
if ( leftBorderSum > maxLeftBorderSum )
maxLeftBorderSum = leftBorderSum;
}
int maxRightBorderSum = 0, rightBorderSum = 0;
for ( int j = center + 1; j <= right; j++ )
{
rightBorderSum += a[ j ];
if ( rightBorderSum > maxRightBorderSum )
maxRightBorderSum = rightBorderSum;
}
return max3( maxLeftSum, maxRightSum,
maxLeftBorderSum + maxRightBorderSum );
}
/**
* Driver for divide-and-conquer maximum contiguous
* subsequence sum algorithm.
*/
int maxSubSum3( const vector<int> & a )
{
return maxSumRec( a, 0, a.size( ) - 1 );
}
/**
* Linear-time maximum contiguous subsequence sum algorithm.
*/
int maxSubSum4( const vector<int> & a )
{
int maxSum = 0, thisSum = 0;
for ( int j = 0; j < a.size( ); j++ )
{
thisSum += a[ j ];
if ( thisSum > maxSum )
maxSum = thisSum;
else if ( thisSum < 0 )
thisSum = 0;
}
return maxSum;
}
/*
最大正子序列 所有的sub 都为正数
*/
int maxSubPositiveSum( const vector<int> & a)
{
int maxSum = 0, thisSum = 0,i;
bool isPositive[8];
for (i = 0 ;i < 8 ; i ++)
isPositive[i] = a[i]>0?true:false;
for ( int j = 0; j < a.size( ); j++ )
{
if ( isPositive[j])
{
thisSum += a[ j ];
if ( thisSum > maxSum )
maxSum = thisSum;
else if ( thisSum < 0 )
thisSum = 0;
}
else if (!isPositive[j] )
thisSum = 0;
}
return maxSum;
}
int main( )
{
vector<int> a( 8 );
a[ 0 ] = 4;a[ 1 ] = -3;a[ 2 ] = 5;a[ 3 ] = -2;
a[ 4 ] = -1;a[ 5 ] = 2;a[ 6 ] = 6;a[ 7 ] = -2;
int maxSum,i;
maxSum = maxSubSum3( a );
cout << "Max sum is " << maxSum << endl;
maxSum = maxSubPositiveSum( a );
cout << "Max sum is " << maxSum << endl;
return 0;
}
posted on 2010-06-13 23:19
付翔 阅读(226)
评论(0) 编辑 收藏 引用 所属分类:
数据结构与算法分析 C++ 描述