알고리즘 문제풀이
[백준/BOJ] 백준 7785번 : 회사에 있는 사람
GeniusJo
2020. 9. 23. 02:33
7785번: 회사에 있는 사람
첫째 줄에 로그에 기록된 출입 기록의 수 n이 주어진다. (2 ≤ n ≤ 106) 다음 n개의 줄에는 출입 기록이 순서대로 주어지며, 각 사람의 이름이 주어지고 "enter"나 "leave"가 주어진다. "enter"인 경우는 �
www.acmicpc.net
set을 이용하여 회사에 어떤 사람이 있는지 관리하였다. 출력은 사전 순의 역순으로 출력하므로 reverse_iterator를 사용하여 출력했다.
코드
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
using namespace std;
int main()
{
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n;
set<string> people;
set<string>::iterator it;
set<string>::reverse_iterator r_it;
string input;
string command;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> input;
cin >> command;
if (command == "enter")
people.insert(input);
else if (command == "leave")
{
it = people.find(input);
people.erase(it); //해당 사람을 people에서 지운다
}
}
//사전 순의 역 순으로 출력한다
for (r_it = people.rbegin(); r_it != people.rend(); r_it++)
cout << *r_it << "\n";
return 0;
}