No Rules Rules

괄호 (feat. 백준, 9012번) 본문

생활/코테

괄호 (feat. 백준, 9012번)

개발하는 완두콩 2022. 8. 11. 22:31
728x90
반응형

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

 

9012번: 괄호

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

www.acmicpc.net

 

반응형

 

// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
int main() {
	ios::sync_with_stdio(false), cin.tie(NULL);
	register int N;
	stack<char> ans;
	string str;
	cin >> N;
	for (register int n = 0; n < N; ++n) {
		cin >> str;
		for (register int i = 0; i < str.size(); ++i) {
			if (str.at(i) == ')')
				if (!ans.empty() && ans.top() == '(')
					ans.pop();
				else {
					ans.push(str.at(i));
					break;
				}
			else
				ans.push(str.at(i));
		}
		if (!ans.empty())
			cout << "NO" << "\n";
		else
			cout << "YES" << "\n";
		while (!ans.empty())
			ans.pop();
	}
	return 0;
}
// *&)*@*

입력 문자가 ')' 라면 스택의 가장 위가 '('인지 확인합니다. 만약 스택이 empty 상태가 아니고 '('가 있다면 pop을 하고 없다면 종료한 뒤 "NO"를 출력해야 합니다.

728x90
반응형

'생활 > 코테' 카테고리의 다른 글

스택 수열 (feat. 백준, 1874번)  (0) 2022.08.12
균형잡힌 세상 (feat. 백준, 4949번)  (0) 2022.08.12
제로 (feat. 백준, 10773번)  (0) 2022.08.11
스택 (feat. 백준, 10828번)  (0) 2022.08.11
주유소 (feat. 백준, 13305번)  (0) 2022.08.11
Comments