Skip to content

[C++] BOJ4949 (stack의 활용) #17

@Settpark

Description

@Settpark
  • BOJ4949 구현은 어렵지 않았는데 아래의 코드에서
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    while (1) {
        string input;
        getline(cin, input);
        if (input == ".") break;
        stack<char> st;
        
        bool isVaild = true;
        for (auto c: input) {
            if (c=='(' || c == '[') {
                st.push(c);
            }
            else if (c == ')') {
                if (st.empty() || st.top() != '(') {
                    isVaild = false;
                    break;
                }
                st.pop(); // *1
            }
            else if (c == ']') {
                if (st.empty() || st.top() != '[') {
                    isVaild = false;
                    break;
                }
                st.pop(); // *2
            }
            // st.pop() *3
        }
        if (isVaild == false || !st.empty()){
            cout << "no" << '\n';
        }
        else {
            cout << "yes" << '\n';
        }
    }
  • *3번만 처리해서 문제가 생김!
  • *1 *2번으로 따로따로 ) 들어올때만 pop() 해야 하는 데,
  • 들어오지 않았는 데도 pop()을 해버려서 와장창 오답이 나옴.
  • (), {} 다루는 문제였는 데, 나도 그걸 좀 잘 보자...

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions