티스토리 뷰

Algorithm/BOJ

백준 7785번 C++

poopooreum 2023. 8. 22. 15:18
반응형
백준 7785번 회사에 있는 사람

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

7785번: 회사에 있는 사람

첫째 줄에 로그에 기록된 출입 기록의 수 n이 주어진다. (2 ≤ n ≤ 106) 다음 n개의 줄에는 출입 기록이 순서대로 주어지며, 각 사람의 이름이 주어지고 "enter"나 "leave"가 주어진다. "enter"인 경우는

www.acmicpc.net



정답 코드

#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstring>
#include <vector>
#include <cctype>

using namespace std;
map <string, string> member;
map <string, string, greater<string>> enter_log;

int main() {

    int N;
    string name, state;
    cin >> N;

    for (int i = 0; i < N; i++) {
        cin >> name >> state;
        member.insert({name, state});
        
        if (member.find(name) != member.end()) {
            if (state == "enter") {
                enter_log.insert({name, name});
            }
            else if (state == "leave") {
                enter_log.erase(name);
            }
        }
        else { 
            if (state == "enter") {
                enter_log.insert({name, name});
            }
        }
    }

    for (auto k : enter_log) {
        cout << k.first << '\n';
    }
    
    return 0;
}

문제 풀이

map을 사용해서 풀었습니다. map은 기본적으로 내림차순이지만 greater를 사용하게 되면 오름차순으로 정렬할 수 있습니다. 그리고 find함수를 사용하였는데 원하는 값이 없을 때에는 map.end()를 반환합니다.
그리고 for문을 each for문 형태를 사용하였는데
편리하고 간결합니다.

반응형

'Algorithm > BOJ' 카테고리의 다른 글

백준 8393번 C++  (0) 2023.08.23
백준 8370번 C++  (0) 2023.08.23
백준 7576번 C++  (0) 2023.08.22
백준 7569번 C++  (0) 2023.08.22
백준 7568번 C++  (0) 2023.08.22
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함