[백준] 1918 후위 표기식
#include <algorithm>
#include <stack>
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
map<char, int> rank;
rank['('] = 0;
rank['+'] = 1; rank['-'] = 1;
rank['*'] = 2; rank['/'] = 2;
string inorder;
stack <char> st;
cin >> inorder;
for (int i = 0; i < inorder.length(); ++i) {
char ch = inorder[i];
if (ch >= 'A' && ch <= 'Z') cout << ch;
else if (ch == '(') st.push('(');
else if (ch == ')') {
while (true) {
if (st.top() == '(') {
st.pop();
break;
}
cout << st.top();
st.pop();
}
}
else{
while (!st.empty()) {
if (rank[st.top()] < rank[ch]) break;
cout << st.top();
st.pop();
}
st.push(ch);
}
}
while (!st.empty()) {
cout << st.top();
st.pop();
}
return 0;
}
📌참고자료