끄적끄적 코딩
article thumbnail
Published 2019. 9. 10. 19:07
[C++] 백준 3425번 고스택 알고리즘

스택에 여러가지 기능을 더 추가한 문제입니다.

함수를 입력받고 숫자 testcase를 입력받습니다.
그리고 수를 입력 받는데
이 수는 처음 stack에 들어가게 될 수이며
입력 받은 함수들을 순서대로 실행시켜서
나오는 결과를 출력하여야 합니다.

testcase만큼 반복하고 다시 함수를 받는 상태로 돌아갑니다.
이는 QUIT를 입력받을 때 까지 계속 반복해서 진행됩니다.

오류가 날 경우 ERROR를 출력해줍니다.
(0으로 나누거나, 값을 초과하거나, 결과값이 하나가 아니거나, 비어있는데 호출하는 등)
그 외에는 결과값을 출력합니다.


#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#define MAX 1000000000
using namespace std;
typedef long long ll;

stack<ll> s;
vector<string> v;
vector<ll> numStore;

int NUM(ll x)
{
	s.push(x);

	return 0;
}

int POP()
{
	if (s.empty()) {
		return 1;
	}

	s.pop();

	return 0;
}

int INV()
{
	if (s.empty()) {
		return 1;
	}

	ll x = -(s.top());
	s.pop();
	s.push(x);

	return 0;
}

int DUP()
{
	if (s.empty()) {
		return 1;
	}

	s.push(s.top());

	return 0;
}

int SWP()
{
	if (s.empty()) {
		return 1;
	}

	ll x = s.top();
	s.pop();

	if (s.empty()) {
		return 1;
	}

	ll y = s.top();
	s.pop();
	s.push(x);
	s.push(y);

	return 0;
}

int ADD()
{
	if (s.empty()) {
		return 1;
	}

	ll x = s.top();
	s.pop();

	if (s.empty()) {
		return 1;
	}

	x += s.top();
	s.pop();
	s.push(x);

	return 0;
}

int SUB()
{
	if (s.empty()) {
		return 1;
	}

	ll x = s.top();
	s.pop();

	if (s.empty()) {
		return 1;
	}

	x = s.top() - x;
	s.pop();
	s.push(x);

	return 0;
}

int MUL()
{
	if (s.empty()) {
		return 1;
	}

	ll x = s.top();
	s.pop();

	if (s.empty()) {
		return 1;
	}

	x *= s.top();
	s.pop();
	s.push(x);

	return 0;
}

int DIV()
{
	int neg = 0;

	if (s.empty()) {
		return 1;
	}

	ll x = s.top();

	if (x < 0) {
		++neg;
	}

	s.pop();

	if (s.empty()) {
		return 1;
	}

	ll y = s.top();

	if (y < 0) {
		++neg;
	}

	if (x == 0) {
		return 1;
	}

	x = y / x;
	if (neg == 1) {
		x = -(abs(x));
	}
	else {
		x = abs(x);
	}

	s.pop();
	s.push(x);

	return 0;
}

int MOD()
{
	int neg = 0;

	if (s.empty()) {
		return 1;
	}

	ll x = s.top();

	s.pop();

	if (s.empty()) {
		return 1;
	}

	ll y = s.top();

	if (y < 0) {
		++neg;
	}

	if (x == 0) {
		return 1;
	}

	x = y % x;
	if (neg == 1) {
		x = -(abs(x));
	}
	else {
		x = abs(x);
	}

	s.pop();
	s.push(x);

	return 0;
}

int main(int argc, char* argv[])
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n;
	int first;
	int errChk;
	int c;
	ll x;
	string str;

	while (1) {
		errChk = 0;
		while (1) {
			cin >> str;

			if (str == "NUM") {
				cin >> x;
				numStore.push_back(x);
			}
			if (str == "QUIT") {
				return 0;
			}
			if (str == "END") {
				break;
			}

			v.push_back(str);
		}

		cin >> n;

		while (n--) {
			c = 0;

			cin >> first;
			
			s.push(first);

			for (int i = 0; i < v.size(); ++i) {
				if (v[i] == "NUM") {
					errChk = NUM(numStore[c]);
					++c;
				}
				else if (v[i] == "POP") {
					errChk = POP();
				}
				else if (v[i] == "INV") {
					errChk = INV();
				}
				else if (v[i] == "DUP") {
					errChk = DUP();
				}
				else if (v[i] == "SWP") {
					errChk = SWP();
				}
				else if (v[i] == "ADD") {
					errChk = ADD();
				}
				else if (v[i] == "SUB") {
					errChk = SUB();
				}
				else if (v[i] == "MUL") {
					errChk = MUL();
				}
				else if (v[i] == "DIV") {
					errChk = DIV();
				}
				else if (v[i] == "MOD") {
					errChk = MOD();
				}

				if (!s.empty() && (s.top() > MAX || s.top() < -MAX)) {
					errChk = 1;
				}
				if (errChk == 1) {
					break;
				}
			}

			if (errChk == 1 || s.size() != 1) {
				cout << "ERROR\n";
			}
			else {
				cout << s.top() << "\n";
			}
			while (!s.empty()) {
				s.pop();
			}
		}

		numStore.clear();
		v.clear();
		cout << endl;
	}

	return 0;
}

'알고리즘' 카테고리의 다른 글

[C++] 백준 2178번 미로 탐색  (0) 2019.09.10
[C++] 백준 2606번 바이러스  (0) 2019.09.10
[C++] 백준 1655번 가운데를 말해요  (0) 2019.09.10
[C++] 백준 11286번 절댓값 힙  (0) 2019.09.10
[C++] 백준 1927번 최소 힙  (0) 2019.09.09

검색 태그