연속되는 소괄호 close는 바로 앞단에 소괄호 open이 와야하니까.
여기서 stack을 사용해야 겠다 생각함.
#include <iostream>
#include <list>
using namespace std;
#include <map>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
// 4949 균형 잡힌 세상.
// 15:22 ~ 15:45
int main()
{
// 소괄호 변수
// 대괄호 변수
// 만들어서 카운팅을 진행함.
while (1)
{
string s;
//cin >> s;
getline(cin, s);
if (s[0] == '.')
{
return 0;
}
// 풀이전략 변경함. : 15:41 ~ 15:45
stack<char>sta;
int soCnt = 0;
int daeCnt = 0;
bool check = false;
for (auto iter : s)
{
if (iter == '(')
{
++soCnt;
sta.push('(');
}
else if (iter == ')')
{
--soCnt;
if (!sta.empty())
{
if (sta.top() == '(')
{
sta.pop();
}
else
{
check = true;
cout << "no" << endl;
break;
}
}
}
else if (iter == '[')
{
++daeCnt;
sta.push('[');
}
else if (iter == ']')
{
--daeCnt;
if (!sta.empty())
{
if (sta.top() == '[')
{
sta.pop();
}
else
{
check = true;
cout << "no" << endl;
break;
}
}
}
if (soCnt < 0 || daeCnt < 0)
{
cout << "no" << endl;
check = true;
break;
}
// 짝짝이 일 경우에 no 출력해야 함.
// 5번째 예제 해당함.
}
if (check == false)
{
if (soCnt == 0 && daeCnt == 0)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
}
}
}