[백준/c++] 1874번: 스택 수열

somyeong·2022년 6월 4일
0

백준

목록 보기
33/45
post-thumbnail

문제 링크 - https://www.acmicpc.net/problem/1874

🌱 문제

🌱 풀이 및 코드

///1874. 스택 수열
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
using namespace std;

int n;
vector<char> answer; //정답 보관 벡터
stack<int> st; 
queue<int> q;
bool flag; //불가능 여부 저장


int main(){
    flag= true;
    cin>>n;
    for(int i=0; i<n; i++){ //주어진 수열 queue에 담기
        int x;
        cin>>x;
        q.push(x);
       
    }

    int index=1; //1부터 N까지 차례대로 stack에 push하므로 push 하는 숫자저장.
    int cur=0; //stack의 top 값 
    while(!q.empty()){
        if(cur<q.front()){
            st.push(index);
            index ++;
            cur=st.top();
            answer.push_back('+');
        }
        else if(cur>=q.front()){
            if(st.top()!=q.front()){ //더이상 push 할 수 없을때, st.top이 q.front와 다르면 불가능한 경우이다.
                flag=false; //불가능한경우 flag 갱신하고 while문 끝내기.
                break;
            }
            st.pop();
            answer.push_back('-');
            q.pop();
            if(!st.empty()) //이 if문 없었더니 segmentation 오류났음.
                cur=st.top();
            
        }

    }
    if(flag==false) 
        cout<<"NO"<<"\n";
    else
        for(int i=0; i<answer.size(); i++){ //정답벡터 차례대로 출력
            cout<<answer[i]<<"\n";
        }
}

🌱 느낀점

  • 너무 복잡하게 푼것 같아서 다른 풀이들을 찾아 보았다.
  • 해당 코드를 보니 queue까지 이용 안하고, stack 하나로 비교적 간단하게 풀 수 있어서 참고하였다 .!! 좋은 아이디어 인것 같다.

참고 블로그 - https://gaeunhan.tistory.com/16

profile
공부한 내용 잊어버리지 않게 기록하는 공간!

0개의 댓글