[c++] 백준 18단계

8002 단어 알고리즘CC

#10828

#include <iostream>
#include <string>
#include <stack>
using namespace std;


int main()
{
	int n, num;
	stack<int> myStack;
	string str;
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		cin >> str;
		
		if (str == "push")
		{
			cin >> num;
			myStack.push(num);
		}
		else if (str == "pop")
		{
			if (!myStack.empty())
			{
				cout << myStack.top() << "\n";
				myStack.pop();
			}
			else
			{
				cout << "-1" << "\n";
			}
		}
		else if (str == "size")
		{
			cout << myStack.size() << "\n";
		}
		else if (str == "empty")
		{
			if (myStack.empty())
			{
				cout << "1" << "\n";
			}
			else
			{
				cout << "0" << "\n";
			}
		}
		else if (str == "top")
		{
			if (myStack.empty())
			{
				cout << "-1" << "\n";
			}
			else
			{
				cout << myStack.top() << "\n";
			}
		}
	}

	return 0;
}

#10773

#include <iostream>
#include <stack>
using namespace std;


int main()
{
	stack<int> myStack;
	int k, num, sum = 0;
	cin >> k;

	for (int i = 0; i < k; i++)
	{
		int n;
		cin >> n;

		if (n == 0)
		{
			myStack.pop();
		}
		else
		{
			myStack.push(n);
		}
	}

	int size = myStack.size();

	for (int i = 0; i < size; i++)
	{
		num = myStack.top();
		sum += num;
		myStack.pop();
	}

	cout << sum;

	return 0;
}

#9012

#include <iostream>
#include <string>
#include <stack>
using namespace std;


int main()
{
	stack<int> myStack;
	string str;
	int T;
	bool sayNo = false;
	cin >> T;

	for (int i = 0; i < T; i++)
	{
		cin >> str;
		sayNo = false;

		while (!myStack.empty())
		{
			myStack.pop();
		}

		for(int j=0; j<str.length(); j++)
		{
			if (str[j] == '(')
			{
				myStack.push(str[j]);
			}
			else if (str[j] == ')' && !myStack.empty() && myStack.top() == '(')
			{
				myStack.pop();
			}
			else
			{
				cout << "NO" << "\n";
				sayNo = true;
				break;
			}
		}

		if (!myStack.empty())
			cout << "NO" << "\n";
		else if(!sayNo)
			cout << "YES" << "\n";

	}

	return 0;
}

#4949

//분명 맞게 풀었는데 계속 틀렸다고 해서 봤더니 yes를 YES로 써서 틀렸음

#include <iostream>
#include <string>
#include <stack>
using namespace std;


int main()
{
	string str;
	
	while (1)
	{
		getline(cin, str);

		if (str == ".")
			break;

		bool sayNo = false;
		
		stack<int> myStack;

		for (int i = 0; i < str.length(); i++)
		{
			if (str[i] == '(' || str[i] == '[')
			{
				myStack.push(str[i]);
			}
			else if (str[i] == ')')
			{
				if (!myStack.empty() && myStack.top() == '(')
					myStack.pop();
				else
				{
					sayNo = true;
					break;
				}
			}
			else if (str[i] == ']')
			{
				if (!myStack.empty() && myStack.top() == '[')
					myStack.pop();
				else
				{
					sayNo = true;
					break;

				}
			}
		}

		if (!sayNo && myStack.empty())
			cout << "yes" << "\n";
		else
			cout << "no" << "\n";

	}

	return 0;
}

#1874

#include <iostream>
#include <string>
#include <stack>
using namespace std;


int main()
{
	stack<int> MyStack;
	int n;
	int lastIn = 0;
	string str;

	cin >> n;

	for (int i = 1; i <= n; i++)
	{
		int m;
		cin >> m;

		while (lastIn < m)
		{
			MyStack.push(++lastIn);
			str.push_back('+');
		}

		if (MyStack.top() == m)
		{
			MyStack.pop();
			str.push_back('-');
		}
		else
		{
			cout << "NO" << "\n";
			return 0;
		}
	}

	for (auto x : str)
		cout << x << "\n";

	return 0;
}

#17298

//구글링함

#include <iostream>
#include <vector>
#include <stack>
using namespace std;


int main()
{
	stack<int> MyStack;
	int N, temp;
	
	cin >> N;

	vector<int> input_vec(N), result_vec(N, -1);


	for (int i = 0; i < N; i++) {
		cin >> input_vec[i];
	}
	
	for (int i = 0; i < N; i++)
	{
		while(!MyStack.empty() && input_vec[i] > input_vec[MyStack.top()])
		{
			result_vec[MyStack.top()] = input_vec[i];
			MyStack.pop();
		}

		if (MyStack.empty())
			result_vec[i] = -1;

		MyStack.push(i);
	}

	for (int i = 0; i < N; i++)
		cout << result_vec[i] << " ";

	return 0;
}

좋은 웹페이지 즐겨찾기