|
#include <string> |
|
#include <vector> |
|
#include <algorithm> |
|
#include <iostream> |
|
using namespace std; |
|
|
|
vector<long long> result; |
|
|
|
|
|
void findMaxnum(vector<long long> num, vector<char> operation, vector<char> oper, bool check[3]){ |
|
// ์ซ์ ํ๋๋ง ๋จ์์ ๊ฒฝ์ฐ |
|
if(num.size()==1){ |
|
result.push_back(abs(num[0])); |
|
} |
|
|
|
// ์ซ์ ํ๋ ์ด์ ๋จ์์ ๊ฒฝ์ฐ, ์ฆ ์ฐ์ฐ์ด ๋ ํด์ผํ๋ ๊ฒฝ์ฐ |
|
else{ |
|
// ์ฐ์ฐ์ ์ข
๋ฅ๋ฅผ ํ์ธํ์ฌ backtraking์ผ๋ก ๊ตฌํ |
|
for(int i=0;i<oper.size();i++){ |
|
if(!check[i]){ |
|
check[i]=true; |
|
|
|
vector<long long> tempnum = num; // ํ์ฌ ์ซ์ ๋ฐฐ์ด |
|
vector<long long> newnum; // ๋์ค ์ซ์ ๋ฐฐ์ด |
|
vector<char> newoperation; // ๋์ค ์ฐ์ฐ ๋ฐฐ์ด |
|
|
|
// ์ฐ์ฐ ๋ฐฐ์ด๋ค์ ๋ชจ๋ ์ํํ์ฌ ํ์ฌ ์ฐ์ฐ์ ์ข
๋ฅ์ ๊ฐ์ ๊ฒฝ์ฐ๋ฅผ ์ฐพ๋๋ค |
|
for(int j=0;j<operation.size();j++){ |
|
// ๊ฐ์ ๊ฒฝ์ฐ |
|
// ์ฐ์ฐ์์ ๊ฐฏ์์ +1๋งํผ ์ซ์ ๊ฐฏ์๊ฐ ์๋ค. |
|
// ๋ํ ๊ฐ์ ์ฐ์ฐ์์ผ ๊ฒฝ์ฐ ์์ด ์ฐ์ ์์์ด๊ธฐ ๋๋ฌธ์ ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์ ๋ฐฐ์ด์ ๋๋ค. |
|
if(operation[j] == oper[i]){ |
|
if(oper[i] == '*'){ |
|
tempnum[j+1] = tempnum[j]*tempnum[j+1]; |
|
} |
|
else if(oper[i] == '-'){ |
|
tempnum[j+1]=tempnum[j]-tempnum[j+1]; |
|
} |
|
else{ |
|
tempnum[j+1]=tempnum[j]+tempnum[j+1]; |
|
} |
|
} |
|
// ๋ค๋ฅผ ๊ฒฝ์ฐ ์ฐ์ฐ์ ๋ค์ ์๋ ์ซ์๋ ๋ ์ด์ ์ฐ์ฐ์ ์ฌ์ฉ๋์ง ์๊ธฐ ๋๋ฌธ์ newnum ๋ฐฐ์ด์ ์ถ๊ฐ |
|
// operation๋ ์ฐ์ฐ์ด ์งํํ์ง ์์๊ธฐ ๋๋ฌธ์ newoperation ๋ฐฐ์ด์ ์ถ๊ฐ |
|
else{ |
|
newnum.push_back(tempnum[j]); |
|
newoperation.push_back(operation[j]); |
|
} |
|
} |
|
|
|
newnum.push_back(tempnum[tempnum.size()-1]); // ๋ง์ง๋ง ์ซ์ ๋ฃ๊ธฐ |
|
findMaxnum(newnum,newoperation,oper,check); |
|
|
|
check[i]=false; |
|
} |
|
} |
|
} |
|
} |
|
|
|
long long solution(string expression) { |
|
long long answer=0; |
|
|
|
vector<long long> num; // ์ซ์ ๋ฐฐ์ด |
|
vector<char> operation; // ์ซ์์ฌ์ด์ ๋ชจ๋ ์ฐ์ฐ์ ๋ฐฐ์ด |
|
vector<char> oper; // ์ฐ์ฐ์๋ค์ ์ข
๋ฅ ๋ฐฐ์ด ( ์ค๋ณต x ) |
|
bool check[3]={0}; // dfs๋ก ์์ด ๊ตฌํ (next_permutaion์ผ๋ก๋ ๊ตฌํ ๊ฐ๋ฅ) |
|
|
|
string str=""; |
|
|
|
// expression์ num, operation, oper ๋ฐฐ์ด ์ฑ์ฐ๊ธฐ |
|
for(int i=0;i<expression.size();i++){ |
|
if(isdigit(expression[i])){ |
|
str+=expression[i]; |
|
} |
|
else{ |
|
num.push_back(stoi(str)); |
|
str.clear(); |
|
operation.push_back(expression[i]); |
|
|
|
if(find(oper.begin(),oper.end(),expression[i])==oper.end()){ |
|
oper.push_back(expression[i]); |
|
} |
|
} |
|
} |
|
num.push_back(stoi(str)); |
|
|
|
// dfs๋ก ์์ด๊ตฌํํ๋ ๋ฐฉ์๊ณผ ์ ์ฌํ๊ฒ ํ์ธ |
|
findMaxnum(num,operation,oper,check); |
|
answer = *max_element(result.begin(),result.end()); |
|
return answer; |
|
} |