Published 2020. 12. 29. 05:19
 

9012번: 괄호

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

www.acmicpc.net

#include <iostream>
#include <string>
#include <stack>

using namespace std;

string valid(const string& s) {
	int cnt = 0;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] == '(') {
			cnt++;
		}
		else if (cnt > 0 && s[i] == ')') {
			cnt--;
		}
		else if (cnt <= 0 && s[i] == ')') {
			return "NO";
		}
	}
	if (cnt == 0)
	{
		return "YES";
	}
	else {
		return "NO";
	}

}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	int t; cin >> t; cin.ignore();
	while (t--) {
		string str; cin >> str;
		cout << valid(str) << endl;
	}
}

'Problem set' 카테고리의 다른 글

[백준] 9095: 1, 2, 3 더하기  (0) 2020.12.29
[백준] 9093: 단어 뒤집기  (0) 2020.12.29
[백준] 8958: OX퀴즈  (0) 2020.12.29
[백준] 6588: 골드바흐의 추측  (0) 2020.12.29
[백준] 4673: 셀프넘버  (0) 2020.12.29
복사했습니다!