Coding Test/BaekJoon_C++

백준 28278 <스택 2> C++

JunOnJuly 2024. 2. 8. 17:41
728x90

https://www.acmicpc.net/problem/28278

 

28278번: 스택 2

첫째 줄에 명령의 수 N이 주어진다. (1 ≤ N ≤ 1,000,000) 둘째 줄부터 N개 줄에 명령이 하나씩 주어진다. 출력을 요구하는 명령은 하나 이상 주어진다.

www.acmicpc.net


일반 코드로 실행하면 시간초과가 걸립니다. endl 대신 "\n" 을 사용하고

ios_base::sync_with_stdio(false);
cin.tie(NULL);

 

코드를 추가해줘야 합니다.


#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

vector<string> split(string str, char Delimiter)
{
    istringstream iss(str);
    string buffer;

    vector<string> querys;

    while (getline(iss, buffer, Delimiter))
    {
        querys.push_back(buffer);
    }
    return querys;
}


int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // 스택 선언
    stack<int> nums;
    // 입력 선언 및 초기화
    int N;
    cin >> N;
    // 공백문자 제거
    cin.ignore();
    // 입력 및 실행
    for (int i = 0; i < N; i++)
    {
        // 명령어 입력
        string str;
        std::getline(cin, str, '\n');
        // 명령어 분할
        vector<string> querys = split(str, ' ');
        // 1 명령이면
        if (querys[0] == "1")
        {
            // 스택에 추가
            nums.push(stoi(querys[1]));
        }
        else
        {
            if (querys[0] == "2")
            {
                if (nums.size() != 0)
                {
                    // 맨 위 출력
                    cout << nums.top() << "\n";
                    // 맨 위 제거
                    nums.pop();
                }
                else
                {
                    cout << -1 << "\n";
                }
            }
            else if (querys[0] == "3")
            {
                // 정수의 개수 출력
                cout << nums.size() << "\n";
            }
            else if (querys[0] == "4")
            {
                if (nums.empty())
                {
                    cout << 1 << "\n";
                }
                else
                {
                    cout << 0 << "\n";
                }
            }
            else if (querys[0] == "5")
            {
                if (nums.size() != 0)
                {
                    cout << nums.top() << "\n";
                }
                else
                {
                    cout << -1 << "\n";
                }
            }
        }
    }
}

 

728x90