프로그래밍/문제풀이
[스택] 백준 4949 균형잡힌 세상
하용권
2018. 12. 11. 23:12
https://www.acmicpc.net/problem/4949
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> #include <stack> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); while(1){ string s; stack<char> st; bool chk = true; //yes or no getline(cin, s); if(s[0] == '.') break; int len = s.length(); for(int i = 0; i < len; i++){ if(s[i] == '(' || s[i] == '[') st.push(s[i]); else if(s[i] == ')'){ if(st.empty() || st.top() == '['){ chk = false; break; } else st.pop(); } else if(s[i] == ']'){ if(st.empty() || st.top() == '('){ chk = false; break; } else st.pop(); } } if(chk && st.empty()) cout << "yes" << endl; else cout << "no" << endl; } } | cs |
반응형