您的位置:首页 > 其它

1007(最大子串和)

2013-03-08 21:33 337 查看
#include <iostream>
using namespace std;
const int MAX = 10000;
int a[MAX];
void findij(const int array[], size_t length, unsigned int& i, unsigned int& j, const int& max)
{
int cur_max;
for(i = 0; i <length; i++)
{
cur_max = 0;
for(j=i; j<length; j++)
{
cur_max  += array[j];
if(max == cur_max)
return;
}
}
}
int Kadane(const int array[], size_t length, unsigned int& left, unsigned int& right)
{
unsigned int i, cur_left, cur_right;
int cur_max, max;
cur_max = max = left = right = cur_left = cur_right = 0;
for(i = 0; i < length; ++i)
{
cur_max += array[i];
if(cur_max > 0)
{
cur_right = i;
if(max < cur_max)
{
max = cur_max;
left = cur_left;
right = cur_right;
}
}
else
{
cur_max = 0;
cur_left = cur_right = i + 1;
}
}
return max;
}
int main()
{
unsigned int i, j;
size_t count;
bool flag = true;
cin>>count;
for(i=0; i<count; i++)
{
cin>>a[i];
if(a[i]>=0)
flag = false;
}
if(flag)
{
cout<<0<<" "<<a[0]<<" "<<a[count-1]<<endl;
}
else
{
int max = Kadane(a, count, i, j);
findij(a, count, i, j, max);
cout<<max<<" "<<a[i]<<" "<<a[j]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: