最大子段和详解_最大字段和_Niteip的博客-CSDN博客
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <climits>
#include <unordered_map>
#include <set>
using namespace std;
vector<int> read_line_data();
vector<int> read_line_data(char split);
void output_data(const vector<int>& data);
int main() {
vector<int> a = read_line_data(',');
int n = a.size();
vector<int> b(n, 0);
int max_v = INT_MIN;
int left = 0;
int right = 0;
b[0] = a[0];
for (int i = 1; i < n; i++) {
if (b[i - 1] >= 0) {
b[i] = b[i - 1] + a[i];
} else {
b[i] = a[i];
left = i;
}
if (max_v < b[i]) {
max_v = b[i];
right = i;
}
}
cout << max_v << " " << left << " " << right << endl;
}
vector<int> read_line_data() {
string src;
getline(cin, src);
istringstream iss(src);
vector<int> data;
int token;
while (iss >> token) {
data.push_back(token);
}
return data;
}
vector<int> read_line_data(char split) {
string input;
cin >> input;
vector<int> data;
stringstream ss(input);
string token;
while (getline(ss, token, split)) {
int value = stoi(token);
data.push_back(value);
}
return data;
}
void output_data(const vector<int>& data) {
int n = data.size();
for (int i = 0; i < n; i++) {
cout << data[i] << " ";
}
}