알고리즘

BOJ 7785 : 회사에 있는 사람

minbear 2023. 7. 12. 12:24

#BOJ 7785 : 회사에 있는 사람

 

7785번: 회사에 있는 사람

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

www.acmicpc.net

#풀이

해시를 공부하기 위해 푸는 기본적인 문제이다.

unordered_set 을 가지고 enter이면 삽입하고 leave이면 지운 다음 

사전순의 역순 으로 출력하면 된다.

 

#코드

#include<iostream>
#include<vector>
#include<unordered_set>
#include<algorithm>
using namespace std;
unordered_set<string> m;
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string name, en;
		cin >> name >> en;
		if (en == "leave")
		{
			m.erase(name);
			continue;
		}
		m.insert(name);
	}
	vector<string> v;
	for (auto a : m)
		v.push_back(a);
	sort(v.begin(), v.end(), greater<string>());
	for (auto a : v) {
		cout << a << '\n';
	}
	return 0;
}