Wrong Answer

seokma97 Reply 9 years 3 weeks ago
#include <stdio.h> #include <stack> #include <iostream> using namespace std; stack<char> st; bool isOpen(char ch){ if (ch=='('||ch=='{'||ch=='[') return true; return false; } bool isClose(char ch){ if (ch==')'||ch=='}'||ch==']') return true; return false; } int main() { int n; char ch; string input; cin>>n; for (int i=0;i<n;i++){ cin>>input; for (int j=0;j<input.length();j++){ ch=input[j]; if (isOpen(ch)) st.push(ch); if (isClose(ch)){ if (st.empty()){ st.push(ch); } if(!st.empty() && isOpen(st.top())) st.pop(); if(!st.empty() && !isOpen(st.top())) st.push(ch); } } if (st.empty()){ printf("Y\n"); } else { printf("N\n"); } while (!st.empty()) st.pop(); input.clear(); } return 0; } 오류가 나는 이유를 잘 모르겠습니다.
pichulia Reply 9 years 3 weeks ago
cin >> input;은 줄 하나를 읽는게 아니라 공백이 나올때까지 읽는겁니다. 그래서 입력으로 1 [ ] 이 들어오면 N N 라고 출력되겠죠