求最大子序列和的两种算法

隐藏行号 复制代码 Demo
  1. /*
    
  2. *author: Jack.xu/nanac.xu
    
  3. *date: 08/26/2009
    
  4. *function: max subsequence sum
    
  5. */
    
  6. #include
         
         
    
         
         
  7. #include
         
         
    
         
         
  1. namespace dskit{
    
  2.  
  3. /*with O(N) time complexity*/
    
  4. int MSS(const ELEMENTSTYPE* elements, size_t size)
    
  5. {
    
  6.     assert(NULL != elements);
    
  7.         
    
  8.     int the_sum = 0;
    
  9.     int max_sum = 0;
    
  10.     for(int i = 0; i != size; ++i)
    
  11.     {
    
  12.         the_sum += elements[i];
    
  13.         if(the_sum > max_sum)
    
  14.             max_sum = the_sum;
    
  15.         else if(the_sum < 0)
    
  16.             the_sum = 0;
    
  17.     }
    
  18.     return max_sum;
    
  19. }
    
  20.  
  21. /*divided algorithm with O(NlogN) time complexity*/
    
  22. int MSS_divide(const ELEMENTSTYPE* elements, size_t begin, size_t end)
    
  23. {
    
  24.     assert(NULL != elements && begin  >= 0 && end >= 0);
    
  25.     if(begin == end)
    
  26.     {
    
  27.         if(elements[begin] < 0)
    
  28.             return 0;
    
  29.         else
    
  30.             return elements[begin];
    
  31.     }    
    
  32.  
  33.  
  34.     size_t center = ((begin + end) >> 1);
    
  35.  
  36.     int left_max = MSS_divide(elements, begin, center);
    
  37.     
    
  38.     int right_max = MSS_divide(elements, center + 1, end);
    
  39.  
  40.         
    
  41.     int temp_sum = 0;
    
  42.     int center_left_max = 0;
    
  43.     for(int i = center; i >= begin; i--)
    
  44.     {
    
  45.         if(i < 0)
    
  46.             break;
    
  47.         temp_sum += elements[i];
    
  48.         if(temp_sum > center_left_max)
    
  49.             center_left_max = temp_sum;
    
  50.     }
    
  51.  
  52.     temp_sum = 0;
    
  53.     int center_right_max = 0;
    
  54.     for(int i = center + 1; i <= end; i++)
    
  55.     {
    
  56.         temp_sum += elements[i];
    
  57.         if(temp_sum > center_right_max)
    
  58.             center_right_max = temp_sum;
    
  59.     }
    
  60.  
  61.     temp_sum = (left_max > right_max) ? left_max : right_max;
    
  62.  
  63.     return (temp_sum > (center_right_max + center_left_max)) ? temp_sum : (center_right_max + center_left_max);
    
  64. }
    
  65.  
  66. }
    
  67.  
  68.  
  69. #ifndef NDEBUG
    
  70. int main(int argc, char* argv[])
    
  71. {
    
  72.     ELEMENTSTYPE array[] = {-1, 1, 32, 3, 4, -23, 24};
    
  73.  
  74.     std::cout << dskit::MSS(array, sizeof(array) / sizeof(ELEMENTSTYPE)) << std::endl;
    
  75.     std::cout << dskit::MSS_divide(array, 0, sizeof(array) / sizeof(ELEMENTSTYPE) - 1) << std::endl;
    
  76.     return 0;
    
  77. }
    
  78.  
  79. #endif
    
<script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值