이번 문제도 스택을 사용하는 문제이다.
조건체크를 정확하게 안해서 계속 틀리다가 맞았다.
이런식으로 체크를 해주어도 무방하다.
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
#define endl "\n"
string s;
bool Check(string s)
{
stack<char> st;
bool flag = false;
for (char c : s)
{
if (c == '(') st.push(c);
else if (c == '[') st.push(c);
else if (c == ')')
{
if (st.empty())
{
flag = true; break;
}
else
{
if (st.top() == '(') st.pop();
else if (st.top() == '[' || st.top() == ']' || st.top() == ')')
{
flag = true;
break;
}
}
}
else if (c == ']')
{
if (st.empty())
{
flag = true; break;
}
else
{
if (st.top() == '[') st.pop();
else if (st.top() == ']' || st.top() == ')' || st.top() == '(')
{
flag = true;
break;
}
}
}
}
if (!st.empty() || flag == true) return false;
if (st.empty()) return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
while (1)
{
getline(cin, s);
if (s.size() == 1 && s[0] == '.') break;
if (Check(s)) cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}