Cyclic two-dimensional array maximum sub-array

Source: Internet
Author: User

I. Topics and Requirements

Title: Returns the number of the largest sub-arrays in a two-dimensional integer array.
Requirements:
Enter a two-dimensional shaping array with positive numbers in the array and a negative number.
A two-dimensional array is connected to the end of the line, like a belt.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).

Second, the design idea

By solving the problem of the maximal subarray of a simple one-dimensional loopback array, the method we take is not an O (n) time-complexity algorithm. As for implementing an array loopback, our approach is to resolve the loopback by defining a twice-fold array to access two times to find the array.

Third, the source code

  

1 /*======================================================================2 # Author:tianyongtao && Zhangyapeng3 # e-mail: [Email protected]4 # last modified:2015-04-01 10:145 # Filename:Demo.cpp6 # Description: Finding the largest subarray of two-dimensional loopback arrays7 ======================================================================*/8#include"stdafx.h"9# include <iostream>Ten# include <string> One# define MaxRow - A# define Maxcol - - using namespacestd; - intArr[maxrow][maxcol]; the intarray[maxrow][2*Maxcol]; - intSumarr (intX1,intY1,intX2,inty2)//Sub-array summation - { -     intsum=0; +      for(inti=x1;i<x2;i++) -     { +          for(intj=y1;j<y2;j++) A         { atsum+=Array[i][j]; -         } -     } -     returnsum; - } -  in voidLoadFile (intArr[][maxcol],int& Row,int&Col) - { toFILE * infile = fopen ("D:\\input.txt","R"); +     intnum[ -]; -     intCount=0; the     if(!infile) *     { $cout<<"file read failed!"<<Endl;Panax NotoginsengExit (-1); -     } the     Else +     { A         Charstr[Ten]; theFSCANF (infile,"%[^,]%*c", str); +Row =atof (str); -FSCANF (infile,"%[^,]%*c", str); $Col =atof (str); $          while(!feof (infile)) -         { -FSCANF (infile,"%[^,]%*c", str); thenum[count++]=atof (str); -         }Wuyi     } the      for(inti=row-1; i>=0; i--) -     { Wu          for(intj=col-1; j>=0; j--) -         { Aboutarr[i][j]=num[--Count]; $         } -     } -      for(intI=0; i<row;i++) -     { A          for(intj=0; j<col;j++) +         { theARRAY[I][J] =Arr[i][j]; -Array[i][j+col] =Arr[i][j]; $         } the     } the } the  the //test function - intMain () in { the     introw=0; the     intCol=0; About LoadFile (arr,row,col); the      for(intI=0; i<row;i++) the     { the          for(intj=0; j<col;j++) +         { -cout<<arr[i][j]<<"\ t"; the         }Bayicout<<Endl; the     } the     intX1,y1;//represents the horizontal ordinate of the upper left corner point -     intX2,y2;//represents the horizontal ordinate of the upper right corner point -     intFLAG1,FLAG2,FLAG3,FLAG4;//(FLAG1,FLAG2) identifies the upper-left coordinate (FLAG3,FLAG4) of the largest subarray to identify the lower-right coordinate of the largest subarray theflag1=flag2=flag3=flag4=0; the     intmax = array[0][0];//max value during storage comparison the      for(x1=0; x1<row;x1++) the     { -          for(y1=0; y1<col;y1++) the         { the              for(x2=x1+1; x2<=row;x2++) the             {94                  for(y2=y1+1; y2<=2*col;y2++) the                 { the                     if((y2-y1) >Col) the                     {98                          Break; About                     } -                     if(Sumarr (X1,y1,x2,y2) >max)101                     {102Max =Sumarr (x1,y1,x2,y2);103flag1=X1;104Flag2=Y1; theflag3=x2;106flag4=Y2;107                     }108                 }109             } the         }111     } thecout<<"the maximum number of sub-arrays is:"<<Endl;113      for(inti=flag1;i<flag3;i++) the     { the          for(intj=flag2;j<flag4;j++) the         {117cout<<array[i][j]<<"\ t";118         }119cout<<Endl; -     }121cout<<"The number of the largest sub-arrays is:"<<max<<Endl;122     return 0;123}

Iv. Results of operation

1. File contents

    

2. Running Results

    

V. Results test

1. Full positive

  

2. All negative

  

3. There is a positive negative

  

Six, experience

The program in the implementation of only the last two-dimensional array of the largest sub-array to make a small change, the implementation of the method does not have much change, the only regret is that there is no time to achieve the complexity of O (n).

In the process of knot group development, I cooperate with Tian Yongtao more and more tacit understanding, can improve the development efficiency to a certain extent.

  

Cyclic two-dimensional array maximum sub-array

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.