관리 메뉴

Mini

백준 4949 // 공백포함 한줄입력은 getline(cin,str), 괄호체크 algoritm 본문

Algorithm/boj

백준 4949 // 공백포함 한줄입력은 getline(cin,str), 괄호체크 algoritm

Mini_96 2023. 5. 17. 16:50

4949번: 균형잡힌 세상 (acmicpc.net)

 

4949번: 균형잡힌 세상

각 문자열은 마지막 글자를 제외하고 영문 알파벳, 공백, 소괄호("( )"), 대괄호("[ ]")로 이루어져 있으며, 온점(".")으로 끝나고, 길이는 100글자보다 작거나 같다. 입력의 종료조건으로 맨 마지막에

www.acmicpc.net

마지막끝은 문자열이 "."이면 진짜끝이다.

"    ."은 끝이아님.

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int t;
char c;
stack<char> s;
string str;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL); cout.tie(NULL);

    while (true)
    {
        getline(cin, str);
        if (str == ".")break;
        //cout << str << "\n";
        while(!s.empty()) s.pop();   //스택초기화
        for (char c : str)
        {
            if (c == '(' || c == ')' || c == '[' || c == ']')
            {
                if (!s.empty() && s.top() == '(' && c == ')')
                {
                    s.pop(); continue;
                }
                if (!s.empty() && s.top() == '[' && c == ']')
                {
                    s.pop(); continue;
                }
                /*if (!s.empty() && s.top() == ')' && c == '(') {
                    s.pop(); continue;
                }*/
                s.push(c);
            }
            
        }      
        if (s.size()) cout << "no\n";
        else cout << "yes\n";
    }
    return 0;
}

 * 괄호체크 algoritm 수정

왼쪽괄호 -> push

else(우괄호) -> 빈칸임 -> 오답

                     ->빈칸아님 -> pop(폭발)