Chinaunix首页 | 论坛 | 博客
  • 博客访问: 180159
  • 博文数量: 33
  • 博客积分: 2047
  • 博客等级: 大尉
  • 技术积分: 333
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-21 19:35
文章分类

全部博文(33)

文章存档

2011年(1)

2010年(21)

2009年(11)

分类: C/C++

2010-10-11 13:51:29

最大连续子序列

来源:
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)

Problem Description

给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ...,
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,
例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和
为20。
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该
子序列的第一个和最后一个元素。

Input

测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。

Output

对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元
素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。

Sample Input

6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0

Sample Output

20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0

Hint

Hint
Huge input, scanf is recommended.

Source

浙大计算机研究生复试上机考试-2005年

代码1:
网上来源:

代码2:
#include
#include
#include
#include

int maxSubSequence(int *sequence, int len)
{
    assert(NULL != sequence);

    // sucessive subsequence begin and end element index
    int firstpos, lastpos;
    int beginIndex, endIndex;
    beginIndex = endIndex = -1;

    // 首尾第一个非负整数
    for (beginIndex = 0; beginIndex < len; ++beginIndex)
    {
        if (sequence[beginIndex] >= 0)
        {
            break;
        }
    }
    for (endIndex = len-1; endIndex >= 0; --endIndex)
    {
        if (sequence[endIndex] >= 0)
        {
            break;
        }
    }

    // all element negative or only one non-negative element
    if (beginIndex >= endIndex)
    {
        if (beginIndex == -1 || endIndex == -1)
        {
            printf("max successive subsequence sum, begin and end index: %d %d %d\n",
                0, 0, len-1);
            return 0;
        }
        else if (endIndex >= 0)
        {
            printf("max successive subsequence sum, begin and end index: %d %d %d\n",
                sequence[beginIndex], beginIndex, beginIndex);
            return sequence[beginIndex];
        }
    }

    int tempfirst = -1;
    int templast = -1;
    int maxsum = -1;
    int tempsum = -1;

    // 从beginIndex向endIndex扫描一遍,保存的是max-subsequence第一次出现的位置
    for (int i = beginIndex; i <= endIndex; ++i)
    {
        if (tempsum >= 0)
        {
            ++templast;
            tempsum += sequence[i];
        }
        else
        {
            tempfirst = i;
            templast = i;
            tempsum = sequence[i];
        }

        if (tempsum > maxsum)
        {
            firstpos = tempfirst;
            lastpos = templast;
            maxsum = tempsum;
        }
    }
    printf("max successive subsequence sum, begin and end index: %d %d %d\n",
        maxsum, firstpos, lastpos);

    return maxsum;
}

int main(int argc, char **argv)
{
    // test cases
    int array1[] = {-2, 11, -4, 13, -5, -2};
    maxSubSequence(array1, sizeof(array1)/sizeof(int));

    int array2[] = {-10, 1, 2, 3, 4, -5, -23, 3, 7, -21};
    maxSubSequence(array2, sizeof(array2)/sizeof(int));

    int array3[] = {5, -8, 3, 2, 5, 0};
    maxSubSequence(array3, sizeof(array3)/sizeof(int));

    int array4[] = {10};
    maxSubSequence(array4, sizeof(array4)/sizeof(int));

    int array5[] = {-1, -5, -2};
    maxSubSequence(array5, sizeof(array5)/sizeof(int));

    int array6[] = {0, 0, -2};
    maxSubSequence(array6, sizeof(array6)/sizeof(int));

    return 0;
}

输出结果:
max successive subsequence sum, begin and end index: 20 1 3
max successive subsequence sum, begin and end index: 10 1 4
max successive subsequence sum, begin and end index: 10 2 4
max successive subsequence sum, begin and end index: 10 0 0
max successive subsequence sum, begin and end index: 0 0 2
max successive subsequence sum, begin and end index: 0 0 0


阅读(1794) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~