Repository navigation
Expand file tree
/
Copy path0053.cpp
More file actions
78 lines (67 loc) · 1.62 KB
/
Copy path0053.cpp
File metadata and controls
78 lines (67 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Why Kadane's algorithm works?
// https://afshinm.name/2018/06/24/why-kadane-algorithm-works/
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <stack>
#include <vector>
// #include <utility>
using namespace std;
static const auto _ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
// class Solution {
// public:
int maxSubArray(vector<int>& nums) {
if (nums.empty())
return 0;
int maxsum=nums[0], sum=0;
for (int i=0; i<nums.size(); ++i) {
sum += nums[i];
if (sum > maxsum)
maxsum = sum;
if (sum < 0)
sum = 0;
}
return maxsum;
}
// };
int main() {
string sin, rin;
vector<string> vs;
vector<vector<int>> vn;
vector<int> vr;
ifstream fp;
fp.open("0053.txt");
int d;
int c=0;
while (getline(fp, sin, '\n')) {
vs.push_back(sin);
stringstream ss(sin);
vector<int> vni = {};
while (ss >> d) {
vni.push_back(d);
// printf("%d ", d);
}
vn.push_back(vni);
getline(fp, rin, '\n');
vr.push_back(stoi(rin.c_str(),NULL,10));
// ++c;printf("==%d==\n", c);
}
fp.close();
printf("%s\t%s\t%s\t%s\n", "T","L", "√", "?");
printf("--- --- --- --- \n");
for (int i=0; i<vs.size(); ++i) {
int r = maxSubArray(vn[i]);
printf("%d\t%d\t%d\t%d\n", r==vr[i], 2*i+1, vr[i], r);
// for (int j=0; j<vn[i].size(); ++j) {
// printf("\n%d", vn[i][j]);
// }
}
return 0;
}