1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+
4+ long long solution (string expression) {
5+ long long answer = 0 ;
6+ vector<long long > num;
7+ vector<char > exp, location;
8+ string n = " " ;
9+
10+ for (int i = 0 ; i < expression.size (); i++)
11+ {
12+ if (expression[i] == ' +' || expression[i] == ' -' || expression[i] == ' *' )
13+ {
14+ num.push_back (stoi (n));
15+ n = " " ;
16+ if (find (exp.begin (), exp.end (), expression[i]) == exp.end ())
17+ exp.push_back (expression[i]);
18+ location.push_back (expression[i]);
19+ }
20+ else
21+ n += expression[i];
22+ }
23+
24+ num.push_back (stoi (n));
25+ sort (exp.begin (), exp.end ());
26+
27+ do
28+ {
29+ vector<long long > tmp_num = num;
30+ vector<char > tmp_loc = location;
31+
32+ for (int i = 0 ; i < exp.size (); i++) {
33+ for (int j = 0 ; j < tmp_loc.size (); j++) {
34+ if (exp[i] == tmp_loc[j]) {
35+ if (tmp_loc[j] == ' +' )
36+ tmp_num[j] = tmp_num[j] + tmp_num[j + 1 ];
37+ else if (tmp_loc[j] == ' -' )
38+ tmp_num[j] = tmp_num[j] - tmp_num[j + 1 ];
39+ else if (tmp_loc[j] == ' *' )
40+ tmp_num[j] = tmp_num[j] * tmp_num[j + 1 ];
41+
42+ tmp_num.erase (tmp_num.begin () + j + 1 );
43+ tmp_loc.erase (tmp_loc.begin () + j);
44+ j--;
45+ }
46+ }
47+ }
48+
49+ if (answer < abs (tmp_num[0 ]))
50+ answer = abs (tmp_num[0 ]);
51+ } while (next_permutation (exp.begin (), exp.end ()));
52+
53+ return answer;
54+ }
0 commit comments