Other solutions have been seen in the design and analysis of computer algorithms, but with the highest DP efficiency
Sub-string and
Time limit: ms | Memory limit:65535 KB
Difficulty:3
Describe
Given an integer sequence {A1,a2...,an}, find the continuous non-empty string {ax,ax+1,..., ay}, making the subsequence and the largest, wherein, 1<=x<=y<=n.
Input
The first line is an integer N (n<=10) that represents the number of groups of test data)
The first line of each set of test data is an integer n indicating that the sequence has n integers, followed by n integers I ( -100=<i<=100), representing all the elements in the series. (0<n<=1000000)
Output
For each set of test data output and the maximum contiguous substring of the and.
Sample input
151 2-1 3-2
Sample output
5
My solution.
#include <iostream> #include <climits> #include <stdio.h> #include <vector>using namespace std; int maxsum (const vector<int> &v,const int &m) {int i,lmax,tmax;lmax=-int_max;tmax=0;for (i=0;i<m;i++) {tmax+=v[i];if (Tmax>lmax) lmax=tmax;if (tmax<0) tmax=0;} return Lmax;} int main () {int n,m,i;scanf ("%d", &n), while (n--) {scanf ("%d", &m);vector<int> V (m); for (i=0;i<m;i++) scanf ("%d", &v[i]);p rintf ("%d\n", Maxsum (V,m));} return 0;}
Low-efficiency algorithms
#include <iostream> #include <stdio.h> #include <vector>using namespace std;int maxsum (const vector <int> &v,const int &m) {int i,j,lmax,tmax;lmax=0;for (i=0;i<m;i++) {tmax=0;for (j=i;j<m;j++) {tmax+ =v[j];if (Tmax>lmax) Lmax=tmax;}} return Lmax;} int main () {int n,m,i,num;scanf ("%d", &n), while (n--) {scanf ("%d", &m);vector<int> V (m); for (i=0;i<m;i + +) {scanf ("%d", &num); v[i]=num;} printf ("%d\n", Maxsum (V,m));} return 0;}
Standard process
#include <iostream> #include <climits> #include <cstdio>using namespace Std;int arrmax[1000000]={0}; int main () {int n,m,i,max;scanf ("%d", &n), while (n--) {max=-int_max;scanf ("%d", &m), and for (i=1;i<=m;i++) { scanf ("%d", &arrmax[i]); if (arrmax[i-1]>0) arrmax[i]+=arrmax[i-1];if (Arrmax[i]>max) max=arrmax[i];} printf ("%d\n", Max);} return 0;}
Nyoj 44 substring and (classic DP problem)