1) 문제를 보고, 스택으로 접근할까 생각을 함.
2) 스택으로 하지 않고도, 정수 카운팅으로 처리할 수 있다고 판단함.
#include <iostream>
#include <list>
using namespace std;
#include <map>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
// 9012번 괄호
// 14:57 ~ 15:12
int main()
{
// 입력으로 들어오는 괄호를 통해
// 좌우 대칭이면 yes, 아니면 no
// 스택으로 풀자.
int n;
cin >> n;
for (int i = 0; i < n; ++i)
{
string ss;
cin >> ss;
int cnt = 0;
bool check = false;
for (auto iter : ss)
{
if (iter == '(')
{
++cnt;
}
else
{
--cnt;
}
if (cnt < 0)
{
cout << "NO" << endl;
check = true;
break;;
}
}
if (check == true)
continue;
if (cnt == 0)
{
cout << "YES" << endl;
}
else
cout << "NO" << endl;
}
}