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;
}
}
'Algorithm > 백준' 카테고리의 다른 글
C++ 알고리즘 - 백준 10828 스택 (0) | 2024.12.31 |
---|---|
C++ 알고리즘 - 백준 10773 제로 (0) | 2024.12.31 |
C++ 알고리즘 - 백준 4949 균형잡힌 세상 (0) | 2024.12.31 |
C++ 알고리즘 - 백준 2839 설탕 배달 (0) | 2024.12.31 |
C++ 알고리즘 - 백준 2164 카드2 (0) | 2024.12.30 |