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 |
Tags
- 그리디
- BFS
- 크루스칼
- 우선순위 큐
- MST
- 조합
- Knapsack
- 스택
- 중복 순열
- 빠른 입출력
- 순열
- 시뮬레이션
- 링크드리스트
- 분할 정복
- 클래스
- 세그먼트 트리
- DP
- lis
- 큐
- 피보나치 수
- 백트래킹
- dfs
- BeautifulSoup
- 비트마스크
- 문자열
- 재귀
- 이분 탐색
- 메모리풀
- SSAFY
- 완전 탐색
Archives
- Today
- Total
작심 24/7
[백준] 3425번 고스택 본문
구현이 귀찮았던 시뮬레이션 문제이다.
들어오는 명령어들을 저장한 후
입력 조건 중에
"명령어가 100,000개를 넘어가는 경우와 스택이 수행될 때, 1,000개 이상의 숫자를 저장하는 경우는 없다."
라는 구문이 있는데
명령어의 개수가 10만 개를 넘지 않는다는 말이었으나
'명령어 개수가 10만개를 넘어갈 때 저장되는 숫자가 1000개를 넘지 않지만
명령어의 개수는 10만개 이상일 수 있다'
라고 이해하고 최대 크기를 모르니 배열은 쓰지 못하겠다고 생각하여
각 명령어의 키워드를 한 글자씩 따와서
NUM -> N
SWP -> W
SUB -> S
string order = NWS
이런 식으로 string 변수 order 안에 붙여 넣었었다.
아무튼 굳이 이럴 필요 없이 10만 개 배열로 충분히 명령어를 입력시킬 수 있다는 것을 말하고 싶다.
ERROR가 발생하는 경우인
1. 절댓값이 10억을 넘을 때
2. 0으로 나누려고 할 때
3. 스택의 사이즈가 1이 아닐 때
4. 연산 수행할 숫자가 부족할 때
를 주의해주며 구현하면 된다.
#include <iostream>
#include <stack>
#include <string>
#include <cstdlib>
using namespace std;
#define MAX 1000000000
string tmp;
bool error = false;
int idx;
long long N, num, temp, temp2;
long long NUM[1001];
int main() {
while (1) {
string order;
idx = 0;
while (1) {
cin >> tmp;
if (tmp == "END" || tmp =="QUIT") break;
else if (tmp == "DIV" || tmp == "MUL") order += tmp[2];
else if (tmp == "SWP") order += tmp[1];
else {
if (tmp == "NUM") cin >> NUM[idx++];
order += tmp[0];
}
}
if (tmp == "QUIT") break;
cin >> N;
for (int n = 0; n < N; n++) {
cin >> num;
stack <long long> st;
st.push(num), error = false, idx = 0;
for (int i = 0; i < (int)order.size(); i++) {
switch (order[i])
{
case 'N': //NUM
st.push(NUM[idx++]);
break;
case 'P': //POP
if (st.empty()) error = true;
else st.pop();
break;
case 'I': //INV
if (st.empty()) error = true;
else temp = st.top(), st.pop(), st.push(-1 * temp);
break;
case 'D': //DUP
if (st.empty()) error = true;
else st.push(st.top());
break;
case 'V': //DIV
if (st.size() < 2) error = true;
else {
temp = st.top(), st.pop();
temp2 = st.top(), st.pop();
if (temp == 0) error = true;
else st.push(temp2 / temp);
}
break;
case 'S': //SUB
if (st.size() < 2) error = true;
else {
temp = st.top(), st.pop();
temp2 = st.top(), st.pop();
if (abs(temp2 - temp) > MAX) error = true;
else st.push(temp2 - temp);
}
break;
case 'W': //SWP
if (st.size() < 2) error = true;
else {
temp = st.top(), st.pop();
temp2 = st.top(), st.pop();
st.push(temp), st.push(temp2);
}
break;
case 'M': //MOD
if (st.size() < 2) error = true;
else {
temp = st.top(), st.pop();
temp2 = st.top(), st.pop();
if (temp == 0) error = true;
else st.push(temp2 % temp);
}
break;
case 'L': //MUL
if (st.size() < 2) error = true;
else {
temp = st.top(), st.pop();
temp2 = st.top(), st.pop();
if (abs(temp2 * temp) > MAX) error = true;
else st.push(temp2 * temp);
}
break;
case 'A': //ADD
if (st.size() < 2) error = true;
else {
temp = st.top(), st.pop();
temp2 = st.top(), st.pop();
if (abs(temp2 + temp) > MAX) error = true;
else st.push(temp2 + temp);
}
break;
}
if (error) break;
}
if (error == true || st.size() != 1) cout << "ERROR\n";
else cout << (long long)st.top() << "\n";
}
cout << "\n";
}
return 0;
}
'백준' 카테고리의 다른 글
[백준] 14500번 테트로미노 (1) | 2020.07.06 |
---|---|
[백준] 1103번 게임 (0) | 2020.07.06 |
[백준] 14502번 연구소 (C++, JAVA) (0) | 2020.07.02 |
[백준] 1062번 가르침 (0) | 2020.07.02 |
[백준] 15685번 드래곤 커브 (0) | 2020.07.01 |
Comments