티스토리 뷰

백준

백준 소스코드 [C++] 9012 괄호

Hani_Levenshtein 2020. 8. 27. 19:28

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

 

9012번: 괄호

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

www.acmicpc.net

백준 소스코드 [C++] 9012 괄호

#include <iostream>
#include <stack>
#include<string>
using namespace std;
int main() {
	int n;
	string m;
	stack<char> st;
	cin >> n;
	
	for (int i = 0;i < n;i++) {
		cin >> m;
		for (int j = 0;j < (int)m.length();j++) {
			if (m[j] == '(') st.push(m[j]);
			if (m[j] == ')') {
				if (st.empty()) {
					st.push(m[j]);
					break;
				}
				else  st.pop();
			}
			
		}
		if (st.empty()) cout << "YES" << '\n';
		else {
			cout << "NO" << '\n';
			while (!st.empty()) st.pop();

		}
		
	}
	return 0;
}
댓글