반응형

www.acmicpc.net/problem/9012

 

9012번: 괄호

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

www.acmicpc.net

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

int main(void) {
	int i,T;
	stack<char> s1;
	cin >> T;
	string line;
	for (i = 0;i < T;i++) {
		cin >> line;

		for (int j = 0;j < line.size();j++) {
			char a;
			a = line.at(j);
			if(a == '(')
				s1.push(a);
			else if (a == ')') {
				if (!(s1.empty())) {
					if (s1.top() == '(')
						s1.pop();
				}
				else if (s1.empty())
					s1.push(a);
				else s1.push(a);
			}
		}
		if (s1.empty())cout << "YES"<<'\n';
		else {
			cout << "NO" << '\n';
			while (!s1.empty()) {
				s1.pop();
			}
		}
	}
	return 0;
}

2학년때 후위도 표기로 구현된 계산기를 만들때가 생각난 문제

조금 다른느낌이지만 괄호구현하는거 때문에 애가 탔었던 기억이 난다.

EZ

반응형

'BOJ' 카테고리의 다른 글

[백준 11758번] CCW  (0) 2021.04.30
[백준] 순열 & 조합 (Permutation & Combination)  (0) 2021.04.27
[백준 10773번] 제로  (0) 2021.04.22
[백준 1546번] 평균  (0) 2021.04.22
[백준 1003번] 피보나치 함수  (0) 2021.04.21

+ Recent posts