[백준] 균형잡힌 세상 4949

Soohyeon B·2022년 11월 15일
0

알고리즘 문제 풀이

목록 보기
40/70

문제

  • 문자열이 주어졌을 때 해당 문자열이 균형잡힌 문자열인지 여부를 판단하자.
  • 괄호는 ()와 [] 두 종류이다.
  • 모든 문장은 .으로 끝난다.
  • 입력의 종료조건으로 맨 마지막에 .이 들어온다.
  • 각 줄마다 해당 문자열이 균형을 이루고 있으면 yes, 아니면 no를 출력한다.

풀이

풀이 1

여러 개의 문장을 입력받고 있으므로 getline()을 사용한다.

getline()

  • 원하는 구분자를 만날 때까지 모든 문자열을 입력받아 하나의 string 객체에 저장
  • getline(입력 스트림 오브젝트, 문자열을 저장할 string 객체, 종결 문자);
  • getline(cin, str);
    해당 라이브러리를 사용하면 구분자를 두고 따로 문장을 구분할 필요가 없다.
#include <bits/stdc++.h>
using namespace std;

int main (void){
    ios::sync_with_stdio(0);
    cin.tie(0);
    string str;
    string ans;
    
    while(1){
        //문장 입력받기
        getline(cin, str);
        stack<char> s;
        ans="yes";
        if(str == ".") break;
        //문장 읽기
        for(auto c:str){
            if(c =='['|| c=='(') s.push(c);
            else if(c==')'){
                if(!s.empty() && s.top() =='(') s.pop();
                else ans = "no"; break;
            }
            else if(c==']'){
                if(!s.empty() && s.top() =='[') s.pop();
                else ans = "no"; break;
            }
            
        }
        cout << ans<<'\n';
        
    }

    
    return 0;
}

20%에서 틀렸다!
반례가 있을텐데..

풀이 2

#include <bits/stdc++.h>
using namespace std;

int main (void){
    ios::sync_with_stdio(0);
    cin.tie(0);
    string str;
    string ans;
    
    while(1){
        //문장 입력받기
        getline(cin, str);
        ans="yes";
        
        if(str == ".") break;
        stack<char> s;
        
        //문장 읽기 
        //([]())
        for(auto c:str){ //
            if(c =='['|| c=='(') s.push(c); 
            else if(c==')'){
                if(!s.empty() && s.top() =='(') s.pop();
                else ans = "no"; break;
            }
            else if(c==']'){
                if(!s.empty() && s.top() =='[') s.pop();
                else ans = "no"; break;
            }
            
        }
        //s가 비어있지 않은 경우를 생각해주지않음
        if(!s.empty()) ans = "no";
        cout << ans<<'\n';
        
    }

    
    return 0;
}

예제 입력에서 첫번째 문장이 계속 no가 나온다.
왜????

풀이 3

#include <bits/stdc++.h>
using namespace std;

int main (void){
    ios::sync_with_stdio(0);
    cin.tie(0);
    string str;
    string ans;
    
    while(1){
        //문장 입력받기
        getline(cin, str);
        ans="yes";
        
        if(str == ".") break;
        stack<char> s;
        
        //문장 읽기
        for(auto c:str){ //aaa[(a]
            if(c =='['|| c=='(') s.push(c);
            else if(c==')'){
                if(!s.empty() && s.top() =='(') s.pop();
                else {ans = "no"; break;}
            }
            else if(c==']'){
                if(!s.empty() && s.top() =='[') s.pop();
                else {ans = "no"; break;}
            }
            
        }
        //s가 비어있지 않은 경우를 생각해주지않음
        if(!s.empty()) ans = "no";
        cout << ans<<'\n';
        
    }

    
    return 0;
}

else 문에 여러 문장이 들어가는데 이게 한줄로 들어가더라도
ex)

else ans = "no"; break;

{}중괄호가 없으면 맨 첫번째 문장만 else문으로 취급해준다.

참고

https://kyu9341.github.io/C-C/2020/01/17/C++getline()/

profile
하루하루 성장하는 BE 개발자

0개의 댓글