1. 程式人生 > >PAT 1007 Maximum Subsequence Sum(最大子串和)

PAT 1007 Maximum Subsequence Sum(最大子串和)

原題地址

求出給定數字串的最大子串和,以及這個最大子串和的首尾元素。(若有多個最大子串則取最靠左的那個)

解題思路

本題基本上是最大子串和的裸題,只是增加了一個輸出首尾元素的要求。

最大子串和描述:輸入一個整數序列,求出其中連續子序列和的最大值。

最大子串和幾種解法:

  • 複雜度O(n^3):窮舉所有可能的首尾子串組合,用i和j迴圈確定首尾,k迴圈累加這之間的數,如果這個組合的累加大於記錄過的maxSum則更新。**
  • 複雜度O(n^2):窮舉優化版,只用i迴圈確定子串的首個數,j迴圈不斷延長這個子串,延長的同時累加,如果在累加的某一刻大於了maxSum則更新。
  • 複雜度O(n^2):分治法
    ,將序列分成左右兩部分,分別遞迴求出左,右後,再求跨越中界的最大子序列和。求跨越序列時一直從中間向左/右走到底的同時累加,如果在累加的某一刻大於了maxSum則更新。
  • 複雜度O(n):聯機演算法,只掃描一遍序列,掃描的同時做累加。判斷累加當前元素後的thisSum,如果比記錄過的maxSum大則更新,如果比maxSum小且為負,那麼丟棄構成thisSum的子串,從下一個元素開始另起一個子串。

本題處理

對於這道題我嘗試了O(n)和O(n^2)的方法都可以過,前者耗時13ms後者耗時48ms,資料量小時都已經看得出一些差距了。

需要注意的點:

  • 由於要求輸出首尾元素,那麼就需要記錄下標maxStart和maxEnd,在處理每個子串時,需要nowStart和nowEnd的輔助來更新這兩者。
  • 元素全為正:只要和now有關的變數都初始化為0即可。
  • 元素全為負:題目要求輸出0和最前最尾元素,negative在讀入時判斷有沒有負數。
  • 最大子串只是一個0,那麼maxSum就不能初始化為0,否則無法更新,因此要初始化為負數而且只能是-1,可以在初始化為-999時測試-1 0 2 3就看得出來問題。

AC程式碼

#include <iostream>
using namespace std;

const int maxn = 10005;
int a[maxn];

void maxSubstrSum_n(int n) ///O(n)的聯機演算法
{
    bool negative = true
; //判斷陣列是否全為負數 for (int i = 0; i<n; ++i) { cin >> a[i]; //出現非負數,注意要有等號!有0時,最大值可能為0 if(a[i] >= 0) negative = false; } if (negative) //全為負數 { cout << 0 << ' ' << a[0] << ' ' << a[n-1]; return; } //最大值可能是0,maxSum置為-1才會更新(不能置為-999) int maxSum = -1, maxStart, maxEnd; int nowSum = 0, nowStart = 0, nowEnd = 0; ///核心程式碼 for (int i = 0; i<n; ++i) { nowSum += a[i]; if (nowSum > maxSum) //比之前的maxSum大時更新,由於保留第一組所以不取等號 { maxSum = nowSum; //更新最大值 nowEnd = i; //延伸當前串 maxStart = nowStart; //更新起始位置 maxEnd = nowEnd; } else if (nowSum < 0) //nowSum比maxSum小且為負,另起一個長度為1的子串 { nowSum = 0; nowStart = nowEnd = i+1; //從下一個數開始 } } cout << maxSum << ' ' << a[maxStart] << ' ' << a[maxEnd]; } void maxSubstrSum_n2(int n) ///O(n^2)的遍歷演算法 { bool negative = true; for (int i = 0; i < n; ++i) { cin >> a[i]; //出現非負數,注意要有等號!有0時,最大值可能為0 if (a[i] >= 0) negative = false; } if(negative) //全為負數 { cout << 0 << ' ' << a[0] << ' ' << a[n-1]<< endl; return ; } int maxSum = -1, maxStart = 0, maxEnd = n-1; for (int i = 0; i<n; ++i) { int nowSum = 0; //記錄著以i為起始的所有子串和 for (int j = i; j<n; ++j) { nowSum += a[j]; //i...j if (nowSum > maxSum) //更新maxSum { maxSum = nowSum; maxStart = i; maxEnd = j; } } } cout << maxSum << ' ' << a[maxStart] << ' ' << a[maxEnd] << endl; } int main() { int n; cin >> n; //maxSubstrSum_n(n); maxSubstrSum_n2(n); return 0; }

相關推薦

PAT 1007 Maximum Subsequence Sum大子

原題地址 求出給定數字串的最大子串和,以及這個最大子串和的首尾元素。(若有多個最大子串則取最靠左的那個) 解題思路 本題基本上是最大子串和的裸題,只是增加了一個輸出首尾元素的要求。

PAT - 1007 Maximum Subsequence Sum大子

題目 Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N

01-複雜度2 Maximum Subsequence Sum大子問題變化

一、題目: 01-複雜度2 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is de

PAT--1007 Maximum Subsequence Sum 25 分

1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1, N​2​​ , …, N,​K ​​ }. A continuous subsequence is defined to be { N​

PAT 1007 Maximum Subsequence Sumdp

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } wh

甲級PAT 1007 Maximum Subsequence Sum給出部分坑的測試情況

1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to

PAT 甲級 1007 Maximum Subsequence Sum線上處理演算法優化

1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to

1007 Maximum Subsequence Sum25 分PAT甲級

Problem Description: Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+

PAT (Advanced Level) 1007 Maximum Subsequence Sum 25 分

1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1​​, N​2​​, …, N​K​​ }. A continuous subsequence is defined to be { N​i

PAT甲級】1007 Maximum Subsequence Sum 25 分

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } wher

1007 Maximum Subsequence Sum25 分大連續子序列

題意:求最大連續子序列和並記錄該序列的頭尾元素 #include <bits/stdc++.h> using namespace std; int N; int main() { cin>>N; vector<int>

PAT甲級真題(動態規劃)——1007 Maximum Subsequence Sum 25 分

1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1, N​2 , …, NK }. A continuous subsequence is defined to be { N​i, N​i+

【笨方法學PAT1007 Maximum Subsequence Sum25 分

一、題目 Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ 

PAT甲級1007 Maximum Subsequence Sum C語言實現

code: conclusion: 1、總體思路:最大子列和問題。按陳越姥姥《資料結構》課上有四種演算法,後兩種效率比較高的方法我沒有辦法讓他輸出頭和尾兩個數,所以使用了第二個演算法來改編。首先在每輸入一個數據時,都應該判斷是否是負數,並計數,若所有數都是負數,則輸出

[pat]1007 Maximum Subsequence Sum

CA 應該 else if mes its 結果 printf AI include 經典最大連續子序列,dp[0]=a[0],狀態轉移dp[i]=max(dp[i-1]+a[i],a[i])找到最大的dp[i]. 難點在於記錄起點,這裏同樣利用動態規劃s[i],如果dp[

1007 Maximum Subsequence Sum 25 point(s)

 1007 Maximum Subsequence Sum (25 point(s)) 部分未通過 22分 #include<iostream> #include<vector> #include<algorithm> using namespa

1007 Maximum Subsequence Sum 25 分

1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuou

HDU1003 結題報告大子序列

HDU1003 Max Sum 題解 #include<iostream> #include<algorithm> #include<string> #include<math.h> #include<set> #in

hdu 1081 大子矩陣dp To The Max

Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of

LeetCode刷題記錄——第五十三題大子

題目描述 給定一個整數陣列 nums ,找到一個具有最大和的連續子陣列(子陣列最少包含一個元素),返回其最大和。 示例: 輸入: [-2,1,-3,4,-1,2,1,-5,4], 輸出: 6 解釋: 連續子陣列 [4,-1,2,1] 的和最大,為 6。 思路