본문 바로가기

백준

백준_9012: 괄호

 

www.acmicpc.net/problem/9012

 

9012번: 괄호

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

www.acmicpc.net

 

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

int main() {
	int n;
	string str;
	stack<char> s;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> str;
		for (int j = 0; j < str.size(); j++) {
			if (str[j] == ')' && s.empty() == false) {	//스택 안비워져있고, )가 들어올때
				if (s.top() == '(')			//스택의 마지막이 ( 이면 비우기
					s.pop();
				else
					s.push(str[j]);			// 스택 추가
			}
			else
				s.push(str[j]);			//스택 추가
		}
		if (s.empty() == true)		//스택이 비어있으면 VPS
			cout << "YES\n";
		else
			cout << "NO\n";

		while (!s.empty())		//스택 비우기
			s.pop();
	}

	return 0;
}

 

'백준' 카테고리의 다른 글

백준_ 12865: 평범한 배낭  (0) 2021.01.14
백준_1816: 암호 키  (0) 2021.01.13
백준_1874: 스택수열  (0) 2021.01.07
백준_1003: 피보나치 함수  (0) 2021.01.01
백준_10845: 큐  (0) 2020.12.27