-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_value_arithm_expr.cpp
115 lines (90 loc) · 2.47 KB
/
max_value_arithm_expr.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
using namespace std;
struct data
{
long long min;
long long max;
};
vector<int> operators; // 1:+, 2:-, 3:*
vector<long long> numbers;
long long applyOperation(long long A, long long B, int k)
{
long long result;
switch(operators[k])
{
case 1: result = A + B; break;
case 2: result = A - B; break;
case 3: result = A * B; break;
}
return result;
}
data MinAndMax(int i, int j, vector<vector<long long> > m, vector<vector<long long> > M)
{
long long minValue = LONG_MAX;
long long maxValue = LONG_MIN;
long long A, B, C, D;
for (int k = i; k <= j - 1; ++k)
{
A = applyOperation(M[i][k], M[k+1][j], k);
B = applyOperation(M[i][k], m[k+1][j], k);
C = applyOperation(m[i][k], M[k+1][j], k);
D = applyOperation(m[i][k], m[k+1][j], k);
minValue = min(minValue, min(A, min(B, min(C, D))));
maxValue = max(maxValue, max(A, max(B, max(C, D))));
}
data mm;
mm.min = minValue;
mm.max = maxValue;
return mm;
}
void printMatrix(vector<vector<long long> > M, int n)
{
cout << "------------------" << endl;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cout << M[i][j] << " ";
}
cout << endl;
}
cout << "------------------" << endl;
}
int main()
{
string expresion;
cin >> expresion;
for (int i = 0; i < expresion.length(); ++i)
{
if(expresion[i] != '+' && expresion[i] != '-' && expresion[i] != '*') numbers.push_back(expresion[i] - 48);
else
{
if(expresion[i] == '+') operators.push_back(1);
if(expresion[i] == '-') operators.push_back(2);
if(expresion[i] == '*') operators.push_back(3);
}
}
int n = numbers.size(), j;
vector<vector<long long> > m(n, vector<long long>(n));
vector<vector<long long> > M(n, vector<long long>(n));
for (int i = 0; i < n; ++i)
{
M[i][i] = m[i][i] = numbers[i];
}
data mm;
for (int s = 1; s < n; ++s)
{
for (int i = 0; i < n - s; ++i)
{
j = i + s;
mm = MinAndMax(i, j, m, M);
m[i][j] = mm.min;
M[i][j] = mm.max;
}
}
cout << M[0][n-1] << endl;
return 0;
}