Maximal continuous sub-sequence algorithm (maximum and (end-to-end) of contiguous array of arrays) __ data structure and efficient algorithm

Source: Internet
Author: User

Related description:

The maximum and the sequence of successive subsequence is actually the element and the largest one in the sequence of successive subsequence.

such as a given sequence:
{ -5,-2, 11,-4, 13,-5,-8}
The maximum contiguous subsequence is {11,-4, 13}, and the largest and 20.

method One: Violence O (n^3)

Algorithm Description:

brute force is to enumerate the start and end points of the subsequence, then compute the number of the paragraphs, and then update the maximum by constantly updating the values. But the efficiency is too low.

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100000;
int A[MAXN];
int main ()
{
    int n,max;
    while (scanf ("%d", &n) &&n!=0)
    {for
        (int i=1;i<=n;i++)
        {
            scanf ("%d", &a[i]);
        max=-1111111;
        for (int i=1;i<=n;i++)
        {for
            (int j=i;j<=n;j++)
            {
                int sum=0;
                for (int k=i;k<=j;k++)
                {
                    sum+=a[k];
                }
                if (max<sum) max=sum
            }
        }
       printf ("%d\n", max);
    }
    return 0;
}
Method Two: Optimization method one, preprocessing O (n^2), in fact, is at the beginning with an array sum[i] to represent the previous I and the efficiency is not high.

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100000;
int A[MAXN],SUM[MAXN];
int main ()
{
    int n,max;
    while (scanf ("%d", &n) &&n!=0)
    {
        a[0]=0;
        sum[0]=0;
        for (int i=1;i<=n;i++)
        {
            scanf ("%d", &a[i]);
            Sum[i]=sum[i-1]+a[i];
        }
        max=-1111111;
        for (int i=1;i<=n;i++)
        {for
            (int j=i;j<=n;j++)
            {
                int s=0;
                S=SUM[J]-SUM[I-1];
                if (max<s) max=s
            }
        }
       printf ("%d\n", max);
    }
    return 0;
}
Method Three: The Cumulative traversal algorithm O (n), the efficiency is very high.

The sum is accumulated when the sequence is traversed, and if sum is less than 0 after it is accumulated, the sum is reset and the maximum value of sum is updated each time. Finally, we can find the maximum value.
In fact, the algorithm is divided into several pieces of the sequence, each piece satisfied: For any k, the number of the first k and will not be less than 0 (less than 0 is not the value of the following series), the current I number and greater than the maximum when the update, and the maximum left edge of the block sequence is the first, the right side is the I.
Time complexity is O (n), and can be read side processing, do not need to open an array to save, space is also very province.

The code is as follows:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=100005;
int main ()
{
    int t,n;
    while (scanf ("%d", &n) &&n!=0)
    {
        int max=-1111111,b=-111111;
        for (int i=1;i<=n;i++)
        {
            scanf ("%d", &t);
            if (b<0) b=t;
            else
            {
                b+=t;
            }
            if (B>max)
              max=b;
        }
        printf ("%d\n", max);
    }

Method Four: Dynamic planning O (n), the efficiency is also very high.

algorithm Idea:
Set S[j] represents the maximum and the number of substrings at the end of the A[j by the first J.
Note: Dp[j] is not the sum of the largest contiguous sequence of j-1 numbers, but only the sum of the maximal contiguous sequences containing a[j. We find the maximum value in B[j], that is, the number of the maximal contiguous subsequence.
Recursive formula: Dp[j]=max{dp[j],dp[j-1]+a[j]};

Code:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=10005;
int A[MAXN],DP[MAXN];
int main ()
{
    int n;
    while (scanf ("%d", &n)!=eof&&n!=0)
    {
        for (int i=1; i<=n; i++)
        {
            scanf ("%d", &a [i]);
        int max= dp[1]=a[1];
        for (int i=2; i<=n; i++)
        {
            if (dp[i-1]+a[i]>=a[i))
            {
                dp[i]=dp[i-1]+a[i];
            }
            else
            {
                dp[i]=a[i];
            }
            if (Max<dp[i])
                max=dp[i];
        }
        printf ("%d\n", max);
    }
    return 0;
}

Note: Sometimes it is necessary to find the left and right bounds of the maximal sequence of sequences, and here I give a third method that can be used to HDU1003 the bounds of the code, for example. The rest of the methods are realized and readers can explore for themselves.

Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=1003

Code:

 #include <cstdio> #include <iostream> #include <cstring> using namespace std; const
int maxn=100005;
int A[MAXN];
    int main () {int t,n;
            while (scanf ("%d", &t)!=eof) {for (int k=1;k<=t;k++) {scanf ("%d", &n);
            int max=-1111111,b=-111111;
            int l=1,ll=1,r=1;
                for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);
                    if (b<0) {b=a[i];
                Ll=i;
                } else {b+=a[i];
                    } if (max<b) {max=b;
                    L=ll;
                R=i;
            } if (k<t) printf ("Case%d:\n%d%d%d\n\n", k,max,l,r);

        else printf ("Case%d:\n%d%d%d\n", k,max,l,r); }
    }
}







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.