2023. 6. 8. 22:08γCoding Test/Programmers
πλ¬Έμ 보λ¬κ°κΈ°
π¨π»νμ΄ κ³Όμ
μλ£κ΅¬μ‘° μκ°μ μ μνκΈ°μ(Prefix), μ€μνκΈ°μ(Infix), νμνκΈ°μ(Postfix)μ λν΄ λ°°μ΄ μ μ΄ μμΌλ, μκ°μ΄ μ€λλμ΄ κΈ°μ΅μ΄ μ μ λλλΌκ΅¬μ. κ·Έλμ λ€μ ν λ² μ°Ύμλ΄€μ΅λλ€.
μ»΄νμΌλ¬κ° μ°μ°μ μ°μ μμμ λ°λΌ μ€μνκΈ°μμ νμνκΈ°μμΌλ‘ λ³νν νμ, κ³μ°μ μ§ννλ€κ³ νλλΌκ΅¬μ. μ²μ λ³΄μ§ λͺ»νκ³ λλ¬΄λ§ λ³Ό μ μλ μ»΄ν¨ν° μ μ₯μμ μμμ μΌμͺ½μμ μ€λ₯Έμͺ½μΌλ‘ μ°¨λ‘λλ‘ μ½μ΄κ°λ©° κ³μ°νλ κ² νΈνμ£ . κ·Έμ λ°ν΄, μ¬λμ μ²μ λ³Ό μ μμ΅λλ€. λμΌλ‘ μ 체 μμμ ν λ²μ λ³Ό μ μκΈ° λλ¬Έμ΄μ£ .
λ°λΌμ, μ€μνκΈ°μμ νμνκΈ°μμΌλ‘ λ°κΎΈλ κ³Όμ μ μ§νν΄μΌ ν©λλ€. κ·Έλ¦¬κ³ , μ°μ°μ μ°μ μμ μμ΄λ λ§λ€μ΄μΌ νμ£ .
μ λ λ€μκ³Ό κ°μ μμλλ‘ μ§ννμ΅λλ€.
1. μ£Όμ΄μ§ μμμ νΌμ°μ°μ, μ°μ°μ λ³λ‘ ν ν°ν
μμ μμ μ£Όμ΄μ§ μ λ ₯ "100-200*300-500+20"μΌλ‘ 보μλ©΄, ["100", "-", "200", "*", "300", "-", "500", "+", "20"]μΌλ‘ κ°κ° λΆλ¦¬ν΄μΌ ν©λλ€. λν, μμμ λ±μ₯νλ μ°μ°μμ μ’ λ₯κ° λ¬΄μ 무μμ΄ μλμ§λ μμλμΌ νμ£ . λ±μ₯νλ μ°μ°μκ° μ΅λ 3κ° λΏμ΄λΌ 3! = 6κ°μ§μ μ°μ°μ μ°μ μμ κ²½μ°μ μλ°μ μ λμ€μ§λ§, μ λ κ·Έ λ§μ λ μ€μ΄κ³ μΆμμ΅λλ€.
setμ μ΄μ©νμ¬ λ±μ₯νλ μ°μ°μ μ’ λ₯ μλ₯Ό μ€λ³΅μμ΄ μ μ₯ν΄λ¨κ³ , λΆλ¦¬λ μμμ κ°μ§κ³ μλ Expression ν΄λμ€λ₯Ό λ§λ€μμ΅λλ€.
class Expression {
public:
Expression(const string& expression) {
string digitBuffer;
for (const auto element : expression) {
if (isdigit(element)) {
digitBuffer.push_back(element);
continue;
}
_operatorTypes.insert(element);
_splitedExpression.push_back(digitBuffer);
string _operator;
_operator.push_back(element);
_splitedExpression.push_back(_operator);
digitBuffer.clear();
}
_splitedExpression.push_back(digitBuffer);
}
public:
vector<string> _splitedExpression;
set<char> _operatorTypes;
};
2. μ°μ°μ μ°μ μμ μμ΄ λ§λ€κΈ°
λ±μ₯νλ μ°μ°μ μ’ λ₯ μκ° μ΅λ 3κ°μ΄κΈ° λλ¬Έμ μ΅λ 6κ°μ§μ κ²½μ°μ μκ° μ‘΄μ¬ν©λλ€. μμ΄μ μ¬μ©νκΈ° μν΄ <algorithm> ν€λμ next_permutation()μ μ¬μ©νλλ°, μ€λ¦μ°¨μμΌλ‘ μ λ ¬λ μνμ€ μ»¨ν μ΄λλ₯Ό λ£μ΄μΌ ν¨μκ° μ¬λ°λ₯΄κ² μλν©λλ€.
νμ§λ§ BST κΈ°λ°μΈ setμ μνλ₯Ό μ€λ¦μ°¨μμΌλ‘ μμνμ£ ! κ·Έλμ setμ μννλ©° stringμ μ°¨λ‘λλ‘ λ΄μκ³ , next_permutation()μ κ²°κ³Όλ₯Ό μ°¨λ‘λλ‘ κ²°κ³Όμ λ΄μμ΅λλ€.
vector<map<char, int>> GetOperatorsPermutation(set<char> operators) {
string strOpreators;
vector<map<char, int>> results;
for (const auto _operator : operators)
strOpreators.push_back(_operator);
do {
map<char, int> priority;
for (int i = 0; i < strOpreators.length(); i++)
priority[strOpreators[i]] = i;
results.push_back(priority);
} while (next_permutation(strOpreators.begin(), strOpreators.end()));
return results;
}
3. μ€μνκΈ°μμ νμνκΈ°μμΌλ‘ λ³ννκΈ°
μ΄μ μ£Όμ΄μ§ μ°μ°μ μ°μ μμμ ν ν°ν λ μ€μνκΈ°μλ€μ λ°νμΌλ‘ νμνκΈ°μμ λ§λ€λ©΄ λ©λλ€. μ΅μ’ κ²°κ³Όλ₯Ό λ΄μ vector<string> postFixμ μ°μ°μλ₯Ό λ΄μ λ stack<char> operatorsκ° νμν©λλ€. νμνκΈ°μμ λ§λλ μκ³ λ¦¬μ¦μ λ€μκ³Ό κ°μμ΅λλ€. (πμ°Έκ³ λΈλ‘κ·Έ)
- μ«μλ κ·Έλλ‘ postFixμ μΆκ°
- λ§μ½ μ€νμ΄ λΉμ΄μλ€λ©΄ μ°μ°μλ₯Ό κ·Έλ₯ μ€νμ λ£λλ€.
- (μ€νμ topμ μλ μ°μ°μμ μ°μ μμ < νμ¬ μ°μ°μμ μ°μ μμ) μ΄λ©΄ νμ¬ μ°μ°μλ₯Ό κ·Έλ₯ μ€νμ λ£λλ€.
- (μ€νμ topμ μλ μ°μ°μμ μ°μ μμ >= νμ¬ μ°μ°μμ μ°μ μμ) μ΄λ©΄ 2λ² νΉμ 3λ² μν©μ΄ λ λκΉμ§ pop νμ¬ postFixμ μΆκ°νλ€. λ€ λλ¬λ€λ©΄ νμ¬ μ°μ°μλ₯Ό μ€νμ λ£λλ€.
- λͺ¨λ μμμ λ€ μ¬μ©νλ€λ©΄, μ€νμ΄ λΉ λκΉμ§ popνμ¬ postFixμ μΆκ°νλ€.
vector<string> ConvertInfixToPostFix(const vector<string>& splitedInfixExpression, const map<char, int>& operatorPriority) {
vector<string> postFix;
stack<char> operators;
for (auto element : splitedInfixExpression) {
if (isdigit(element[0])) {
postFix.push_back(element);
continue;
}
if (operators.empty()) {
operators.push(element[0]);
continue;
}
int currentOpratorPriority = operatorPriority.find(element[0])->second;
int stackTopOperatorPriority = operatorPriority.find(operators.top())->second;
if (stackTopOperatorPriority < currentOpratorPriority) {
operators.push(element[0]);
}
else {
while (true) {
if (operators.empty())
break;
char targetOperator = operators.top();
int priority = operatorPriority.find(targetOperator)->second;
if (priority < currentOpratorPriority)
break;
string strOperator;
strOperator.push_back(targetOperator);
postFix.push_back(strOperator);
operators.pop();
}
operators.push(element[0]);
}
}
while (!operators.empty()) {
char targetOperator = operators.top();
operators.pop();
string strOperator;
strOperator.push_back(targetOperator);
postFix.push_back(strOperator);
}
return postFix;
}
4. νμνκΈ°μμ κ³μ°νμ¬ μ΄μ κ³μ° κ²°κ΄κ°λ³΄λ€ ν° μ§ λΉκ΅νλ€.
μ΄μ νμνκΈ°μμ μννλ©° κ³μ°μ νλ©΄ λ©λλ€. κ³μ°νλ κ³Όμ μ μ΄λ ΅μ§ μμμ΅λλ€. μ¬κΈ°μλ μ«μμ μ°μ°μλ₯Ό λ΄μ λ stack<string>μ΄ νμν©λλ€. κ³μ° κ³Όμ μ λ€μκ³Ό κ°μ΅λλ€.
- μ«μλ μ€νμ κ·Έλ₯ μΆκ°νλ€.
- μ°μ°μκ° λμ€λ©΄ μ«μ 2κ°λ₯Ό pop ν΄μ κ³μ°νλ€.
- μ΄λ λ¨Όμ pop λλ μ«μκ° λ λ²μ§Έ κ°, λμ€μ popλλ μ«μκ° μ²« λ²μ§Έ κ°μΌλ‘ νμ¬ κ³μ°ν΄μΌ νλ€. κ³μ°ν κ°μ λ€μ μ€νμ λ£λλ€.
μ κ³Όμ μ νμνκΈ°μμ΄ λλ λκΉμ§ μ§ννλ©΄, μ€νμ μ΅μ’ κ³μ°κ°μ΄ μ μ₯λμ΄ μμ΅λλ€.
long long Calculate(const vector<string>& postFixExpression) {
stack<string> myStack;
for (const auto element : postFixExpression) {
if (isdigit(element[0])) {
myStack.push(element);
continue;
}
long long num2 = stoll(myStack.top());
myStack.pop();
long long num1 = stoll(myStack.top());
myStack.pop();
long long result = 0;
switch (element[0]) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
}
myStack.push(to_string(result));
}
return abs(stoll(myStack.top()));
}
βοΈ μμ€ μ½λ λ° κ²°κ³Ό
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Expression {
public:
Expression(const string& expression) {
string digitBuffer;
for (const auto element : expression) {
if (isdigit(element)) {
digitBuffer.push_back(element);
continue;
}
_operatorTypes.insert(element);
_splitedExpression.push_back(digitBuffer);
string _operator;
_operator.push_back(element);
_splitedExpression.push_back(_operator);
digitBuffer.clear();
}
_splitedExpression.push_back(digitBuffer);
}
public:
vector<string> _splitedExpression;
set<char> _operatorTypes;
};
vector<map<char, int>> GetOperatorsPermutation(set<char> operators) {
string strOpreators;
vector<map<char, int>> results;
for (const auto _operator : operators)
strOpreators.push_back(_operator);
do {
map<char, int> priority;
for (int i = 0; i < strOpreators.length(); i++)
priority[strOpreators[i]] = i;
results.push_back(priority);
} while (next_permutation(strOpreators.begin(), strOpreators.end()));
return results;
}
vector<string> ConvertInfixToPostFix(const vector<string>& splitedInfixExpression, const map<char, int>& operatorPriority) {
vector<string> postFix;
stack<char> operators;
for (auto element : splitedInfixExpression) {
if (isdigit(element[0])) {
postFix.push_back(element);
continue;
}
if (operators.empty()) {
operators.push(element[0]);
continue;
}
int currentOpratorPriority = operatorPriority.find(element[0])->second;
int stackTopOperatorPriority = operatorPriority.find(operators.top())->second;
if (stackTopOperatorPriority < currentOpratorPriority) {
operators.push(element[0]);
}
else {
while (true) {
if (operators.empty())
break;
char targetOperator = operators.top();
int priority = operatorPriority.find(targetOperator)->second;
if (priority < currentOpratorPriority)
break;
string strOperator;
strOperator.push_back(targetOperator);
postFix.push_back(strOperator);
operators.pop();
}
operators.push(element[0]);
}
}
while (!operators.empty()) {
char targetOperator = operators.top();
operators.pop();
string strOperator;
strOperator.push_back(targetOperator);
postFix.push_back(strOperator);
}
return postFix;
}
long long Calculate(const vector<string>& postFixExpression) {
stack<string> myStack;
for (const auto element : postFixExpression) {
if (isdigit(element[0])) {
myStack.push(element);
continue;
}
long long num2 = stoll(myStack.top());
myStack.pop();
long long num1 = stoll(myStack.top());
myStack.pop();
long long result = 0;
switch (element[0]) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
}
myStack.push(to_string(result));
}
return abs(stoll(myStack.top()));
}
long long solution(string expression) {
Expression myExpression(expression);
vector<map<char, int>> operatorPriorities = GetOperatorsPermutation(myExpression._operatorTypes);
long long answer = 0;
for (const auto priority : operatorPriorities) {
vector<string> postFix = ConvertInfixToPostFix(myExpression._splitedExpression, priority);
long long result = Calculate(postFix);
if (answer < result)
answer = result;
}
return answer;
}
κΈ°λ₯λ³λ‘ ν¨μλ₯Ό μ΅λν λΉΌλ €κ³ λ Έλ ₯ μ€μΈλ°, μ½λ κΈΈμ΄κ° μ€νλ € λ κΈΈμ΄μ§λ λλμ λκΉμ...
'Coding Test > Programmers' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Programmers] Lv2. JadenCase λ¬Έμμ΄ λ§λ€κΈ° | C++ (0) | 2023.06.11 |
---|---|
[Programmers] Lv2. 거리λκΈ° νμΈνκΈ° | C++ (0) | 2023.06.09 |
[Programmers] Lv2. λ‘€μΌμ΄ν¬ μλ₯΄κΈ° | C++ (2) | 2023.06.08 |
[Programmers] Lv2. 리μ½μ³ λ‘λ΄ | C++ (0) | 2023.06.07 |
[Programmers] Lv2. κ΄λ¬Ό μΊκΈ° | C++ (0) | 2023.06.06 |