Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Math
- Union-Find
- KMP
- backtracking
- BFS
- FenwickTree
- DP
- topologicalsort
- 2-sat
- Bridge
- LCA
- Tree
- Bellman-Ford
- ArticulationPoint
- MST
- ShortestPath
- Implementation
- LIS
- IndexedTree
- DFS
- Dijkstra
- scc
- Sweeping
- SlidingWindow
- BinarySearch
- Flow
- greedy
- Floyd
- mergesorttree
- TwoPointers
Archives
- Today
- Total
정리충의 정리노트
[백준] 3425: 고스택 본문
0. 문제 주소
https://www.acmicpc.net/problem/3425
1. 풀이
진짜 극혐
DIV와 MOD 연산에서 부호 처리에 관해 주저리주저리 써놓았는데, c++에서 / 과 % 연산자가 알아서 잘 해준다. (처음 알았다)
그거 빼곤 꾹 참고 구현만 하면 되는 문제
2. 풀이 코드
* 유의할 점
1. 스택에서 pop하는 명령어에 대해, empty 상태에서 꺼낼 때의 처리
2. DIV, MOD 유의
#include <bits/stdc++.h>
#define ll long long
#define MAX 1000000000
using namespace std;
vector<string> coms;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
while(true){
string com;
cin >> com;
if (com == "QUIT") break;
string num;
if (com == "NUM"){
cin >> num;
com = com + " " + num;
}
if (com == "END"){
int n;
cin >> n;
for (int x=0;x<n;x++){
stack<ll> st;
ll v;
cin >> v;
st.push(v);
bool error = false;
for (string s: coms){
string curr_com = s.substr(0, 3);
if (curr_com == "NUM"){
st.push(stoll(s.substr(4)));
}
else if (curr_com == "POP"){
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
st.pop();
}
else if (curr_com == "INV"){
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top = st.top();
st.pop();
st.push(top * (-1LL));
}
else if (curr_com == "DUP"){
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top = st.top();
st.push(top);
}
else if (curr_com == "SWP"){
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top1 = st.top();
st.pop();
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top2 = st.top();
st.pop();
st.push(top1);
st.push(top2);
}
else if (curr_com == "ADD"){
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top1 = st.top();
st.pop();
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top2 = st.top();
st.pop();
ll result = top1 + top2;
if (abs(result) > MAX){
error = true;
cout << "ERROR\n";
break;
}
st.push(result);
}
else if (curr_com == "SUB"){
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top1 = st.top();
st.pop();
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top2 = st.top();
st.pop();
ll result = top2 - top1;
if (abs(result) > MAX){
error = true;
cout << "ERROR\n";
break;
}
st.push(result);
}
else if (curr_com == "MUL"){
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top1 = st.top();
st.pop();
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top2 = st.top();
st.pop();
ll result = top2 * top1;
if (abs(result) > MAX){
error = true;
cout << "ERROR\n";
break;
}
st.push(result);
}
else if (curr_com == "DIV"){
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top1 = st.top();
st.pop();
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top2 = st.top();
st.pop();
if (top1 == 0LL) {
error = true;
cout << "ERROR\n";
break;
}
ll result = top2 / top1;
if (abs(result) > MAX){
error = true;
cout << "ERROR\n";
break;
}
st.push(result);
}
else if (curr_com == "MOD"){
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top1 = st.top();
st.pop();
if (st.empty()){
cout << "ERROR\n";
error = true;
break;
}
ll top2 = st.top();
st.pop();
if (top1 == 0LL) {
error = true;
cout << "ERROR\n";
break;
}
ll result = top2 % top1;
if (abs(result) > MAX){
error = true;
cout << "ERROR\n";
break;
}
st.push(result);
}
}
if (error) continue;
if (st.size() != 1){
cout << "ERROR\n";
continue;
}
else{
ll ans = st.top();
cout << ans << "\n";
}
}
cout << "\n";
coms.clear();
}
coms.push_back(com);
}
return 0;
}
Comments