Algorithm/백준
C++ 알고리즘 - 백준 9012 괄호
마루설아
2024. 12. 31. 19:05
https://www.acmicpc.net/problem/9012
#include <iostream>
#include <string>
#include <stack>
#define endl "\n"
using namespace std;
int main(void) {
// C++ Init
ios::sync_with_stdio(false);
cin.tie(NULL);
int input;
string str;
char ch;
cin >> input;
cin.ignore();
for (int i = 0; i < input; i++) {
stack<char> st;
getline(cin, str);
for (int j = 0; j < str.size(); j++) {
if (j == 0 && str[j] == ')') {
cout << "NO" << endl;
break;
}
if (str[j] == '(') {
st.push(str[j]);
continue;
}
if (str[j] == ')') {
if (st.empty()) {
cout << "NO" << endl;
break;
}
ch = st.top();
st.pop();
if (ch == '(') {
if (j == str.size() - 1 && st.empty()) {
cout << "YES" << endl;
break;
}
else continue;
}
}
}
if (!st.empty())
cout << "NO" << endl;
}
}