Stack 1,2,3,4

2022. 6. 22. 16:08Baekjoon

https://www.acmicpc.net/problem/10828

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

10820번

vector로 품

더보기
#include <iostream>
#include <string>
#include <vector>

using namespace std;

 //1. 명령어의 수 2. 둘쨰 줄부터 N개의 줄에는 명령이 하나씩 주어진다.
//주어지는 정수는 1보다 크거나 같고, 100000보다 작거나 같다.
//문제에 ㅏㄴ와있지 않은 명령이 주어지는 경우는 없다.
int main()
{
	std::vector<int> stack;
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int loop;
	cin >> loop;
	
	
	
	for (int i = 0; i < loop; i++) //명령어저장
	{
		std::string command;
	
		 std ::cin >> command;
		if (command == "push")
		{
			int stackdata = 0;
			std::cin >> stackdata;
			stack.push_back(stackdata);
		}
		else if (command == "pop")
		{
			if (stack.empty() == true)
			{
			std::cout << -1 << "\n";
			}
			else
			{ 
			std ::cout << stack.back() << "\n";
				stack.pop_back();
			}
		}
		else if (command == "size")
		{
		std::cout << stack.size() << "\n";
		}
		else if (command == "empty")
		{		
			std:: cout << stack.empty() << "\n";
		}
		else if (command == "top")
		{
			if (stack.empty())
			{
			std::	cout << -1 << "\n";
			}
			else std ::cout << stack.back() << "\n";
		}
	}
				
}

 

오늘 벡터라이브러리랑 내가 어제 연습한 벡터h 함수들이랑 겹쳐서 컴파일이 안된거엿음 예외처리를 잘하자.

 

https://www.acmicpc.net/problem/10773

 

10773번: 제로

첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000) 이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경

www.acmicpc.net

vector로 품

더보기

 

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



int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	std::vector<int>zero;
	int loop = 0;
	std::cin >> loop;
	int number;
	int sum = 0;

	while (loop--)
	{
		std::cin >> number;
		if (number == 0)
		{
			zero.pop_back();
		}else
		zero.push_back(number);
	}
	
    for (vector<int> ::iterator iter = zero.begin(); iter != zero.end(); iter++)
	{
		sum += *iter;
	}
	
    if (zero.empty())
	{
		std::cout << 0 << "\n";
	}
	else std::cout <<sum << "\n";
			
}

 

https://www.acmicpc.net/problem/9012

 

9012번: 괄호

괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고

www.acmicpc.net

 

더보기

 

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


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int loop = 0;
	cin >> loop;
	while (loop--)
	{
	std::vector<char>stack;
		bool ox = false;
		string command;
		cin >> command;
		for (char ch : command)
		{
			if (ch == '(')
			{
				stack.push_back('(');
			}
			else if (stack.empty())
			{
				cout << "NO" << "\n";
				ox = true;
				break;
			}
			else
				stack.pop_back();
		}
		if (!ox)
		{
			if (stack.empty())

				cout << "YES" << "\n";
			else cout << "NO" << "\n";
		}
	}
}

 

 

 

https://www.acmicpc.net/problem/4949

 

 

 

4949번: 균형잡힌 세상

하나 또는 여러줄에 걸쳐서 문자열이 주어진다. 각 문자열은 영문 알파벳, 공백, 소괄호("( )") 대괄호("[ ]")등으로 이루어져 있으며, 길이는 100글자보다 작거나 같다. 각 줄은 마침표(".")로 끝난다

www.acmicpc.net

 

더보기

 

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

int main()
{
	std::ios_base::sync_with_stdio(false); //처음값이 바로 .이면 종료
	std::cin.tie(nullptr);
	std::cout.tie(nullptr);
	while (1)
	{
		string input;
		getline(cin, input); //한줄씩
		stack<char> ch;
		bool box = false; //
		if (input.length() == 1 && input[0] == '.')
		{
			break;
		}
		for (int i = 0; i < input.length(); i++) //처리 부분
		{
			if (input[i] == '(' || input[i] == '[')
			{
				ch.push(input[i]);
			}
			if (input[i] == ')')
			{
				if (ch.empty() || ch.top() == '[')
				{
					box = true;
				}
				else
					ch.pop();
			}if (input[i] == ']')
			{
				if (ch.empty() || ch.top() == '(')
				{
					box = true;
				}
				else
					ch.pop();
			}
		}
		if (ch.empty() && box == false )
		{
			cout << "yes" << "\n";
		}
		else cout << "no" << "\n";


	}
}

 

'Baekjoon' 카테고리의 다른 글

Baekjoon 1157 문제  (0) 2022.05.02
Baekjoon  (0) 2022.05.01
Baekjoon 4344  (0) 2022.05.01
Baekjoon 8958번  (0) 2022.04.30
2675번  (0) 2022.04.29