작성자 없음
작성자 정보가 삭제된 글입니다.
작성
·
74
0
#include <bits/stdc++.h>
using namespace std;
int main(){
string input;
getline(cin,input);
string ret;
while(input!="."){
stack<char> stk;
for(int i=0;i<input.size();i++){
char ch=input.c_str()[i];
if(ch!='('&&ch!=')'&&ch!='['&&ch!=']') continue;
// stk이 비어있는 경우
if(stk.empty()){
// ch가 열린 괄호인 경우
if(ch=='('||ch=='['){
// push(ch);
stk.push(ch);
}
// ch가 닫힌 괄호인 경우
else{
// push(ch); break;
stk.push(ch);
break;
}
}
// stk이 비어있지 않은 경우
else{
// ch가 열린 괄호인 경우
if(ch=='('||ch=='['){
// top()이 닫힌 괄호인 경우 && 형태가 다름
// top()이 닫힌 괄호인 경우 && 형태가 같음
// top()이 열린 괄호 && 형태가 다름
// top()이 열린 괄호 && 형태가 같음
// ==> 위의 4경우 모두 push(ch);
stk.push(ch);
}
// ch가 닫힌 괄호인 경우
else{
// top()이 닫힌 괄호인 경우 && 형태가 다름
// top()이 닫힌 괄호인 경우 && 형태가 같음
// ==> 위의 2경우 모두 push(ch);
if(stk.top()==')'||stk.top()==']'){
stk.push(ch);
}
// top()이 열린 괄호인 경우
else{
// 형태가 다른 경우
if(stk.top()=='('&&ch==']'){
// break;
break;
}
else{
// 형태가 같은 경우
// pop();
stk.pop();
}
}
}
}
}
// stk이 비어있는 경우
if(stk.empty()){
// yes
ret+="yes\n";
}
// stk이 비어있지 않는 경우
else{
// no
ret+="no\n";
}
getline(cin,input);
}
printf("%s",ret.c_str());
return 0;
}
거의 대부분의 반례를 넣어도 통과가 됩니다. 어디서 잘못되었나요?