백준 algorithm

백준 7785 회사에 있는 사람

cosmohoo 2022. 7. 19. 16:14
반응형

문제설명

출력

현재 회사에 있는 사람의 이름을 사전 순의 역순으로 한 줄에 한 명씩 출력한다.

예제 입력 1 복사

4
Baha enter
Askar enter
Baha leave
Artem enter

예제 출력 1 복사

Askar
Artem

 

 

=> 자료구조 <SET>을 사용할 수 있는지 묻는 문제였습니다. 

(C++은 풀때마다 문법때문에 짜증이 나는 경우가 많습니다....물론 제 실력부족이지만.... )

=> cin은 '\n'를 처리하지 않고 입력버퍼에 남겨두기 때문에 getline을 하기전에는 cin.ignore();을 꼭 해주어야합니다.

 

#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include <queue>
#include <utility>
#include <stdio.h>
#include <map>
using namespace std;



int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int N;
    cin >> N;
    set<string, greater<string>> setPeople;
    cin.ignore();
    while(N>0)
    {
        string s;
        
        getline(cin,s);
       
       
        string name = s.substr(0, s.find(" "));
        string state = s.substr(s.find(" ")+1, s.size());
       
        if(state == "enter") //입실
        {
            setPeople.insert(name);
        }
        else if(state == "leave")
        {
            auto iter = setPeople.find(name);
            if(iter != setPeople.end())
            {
                setPeople.erase(iter);
            }
        }
        N--;
    }
    
    for(auto iter : setPeople)
    {
        cout<<iter<<'\n';
    }
}

 

반응형