求最大子序列和的两种算法
2009-12-14 11:01
232 查看
隐藏行号 复制代码 ? Demo
/*
*author: Jack.xu/nanac.xu
*date: 08/26/2009
*function: max subsequence sum
*/
#include
#include
namespace dskit{
/*with O(N) time complexity*/
int MSS(const ELEMENTSTYPE* elements, size_t size)
{
assert(NULL != elements);
int the_sum = 0;
int max_sum = 0;
for(int i = 0; i != size; ++i)
{
the_sum += elements[i];
if(the_sum > max_sum)
max_sum = the_sum;
else if(the_sum < 0)
the_sum = 0;
}
return max_sum;
}
/*divided algorithm with O(NlogN) time complexity*/
int MSS_divide(const ELEMENTSTYPE* elements, size_t begin, size_t end)
{
assert(NULL != elements && begin >= 0 && end >= 0);
if(begin == end)
{
if(elements[begin] < 0)
return 0;
else
return elements[begin];
}
size_t center = ((begin + end) >> 1);
int left_max = MSS_divide(elements, begin, center);
int right_max = MSS_divide(elements, center + 1, end);
int temp_sum = 0;
int center_left_max = 0;
for(int i = center; i >= begin; i--)
{
if(i < 0)
break;
temp_sum += elements[i];
if(temp_sum > center_left_max)
center_left_max = temp_sum;
}
temp_sum = 0;
int center_right_max = 0;
for(int i = center + 1; i <= end; i++)
{
temp_sum += elements[i];
if(temp_sum > center_right_max)
center_right_max = temp_sum;
}
temp_sum = (left_max > right_max) ? left_max : right_max;
return (temp_sum > (center_right_max + center_left_max)) ? temp_sum : (center_right_max + center_left_max);
}
}
#ifndef NDEBUG
int main(int argc, char* argv[])
{
ELEMENTSTYPE array[] = {-1, 1, 32, 3, 4, -23, 24};
std::cout << dskit::MSS(array, sizeof(array) / sizeof(ELEMENTSTYPE)) << std::endl;
std::cout << dskit::MSS_divide(array, 0, sizeof(array) / sizeof(ELEMENTSTYPE) - 1) << std::endl;
return 0;
}
#endif.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i
相关文章推荐
- 最大增长序列的两种算法
- 最大子序列的两种算法
- 最大子序列和算法最优解--Java
- 采用递归求数组里面求最大子序列的算法(手绘图解)
- 求所有最大公共子序列的算法实现
- 树的最小支配集,最小点覆盖,最大独立集两种算法
- 求所有最大公共子序列的算法实现
- 【有趣的面试算法题】之三 求最大子序列和/积问题,关注起点和终点
- 最大连续子序列之和算法进化历程
- 蓝桥杯常用算法知识点:【递归】求两个串的最大公共子序列的长度
- 最大公共子序列(LCS算法)
- 最大子序列和的线性时间算法
- 算法系列之五:最长公共子序列(LCS)问题(非连续子序列)的两种解法
- LCS 最大公共序列算法
- 算法 :最大子序列和问题
- HDU1503:Advanced Fruits LCS 最大公共序列算法
- 【算法与数据结构】最大子序列和问题
- 判断一个序列是否为另一个序列的子序列(两种算法)
- 算法问题(1)最大子序列问题
- 求最大子序列的四种算法,数据结构与算法分析(C语言版)第二章