Coding_Test_C++
BaekJoon 7785번: 회사에 있는 사람(C++)
펄크럼
2021. 6. 16. 11:48
https://www.acmicpc.net/problem/7785
7785번: 회사에 있는 사람
첫째 줄에 로그에 기록된 출입 기록의 수 n이 주어진다. (2 ≤ n ≤ 106) 다음 n개의 줄에는 출입 기록이 순서대로 주어지며, 각 사람의 이름이 주어지고 "enter"나 "leave"가 주어진다. "enter"인 경우는
www.acmicpc.net
풀이법
1) C++의 장점인 STL을 활용하였다.
2) map을 활용한다면 쉽게 풀 수 있다. 중복이 불가능하다는 점을 이용하여 map 객체에 값을 넣어둔다.
3) auto로 반복하여, 역순으로 출력하기 위해서 rbegin() 과 rend()를 사용하였다.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <set>
#include<map>
using namespace std;
int n;
map<string,int> m;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
{
string s1, s2;
cin >> s1 >> s2;
if (s2 == "enter")
{
m[s1]++;
}
else
{
m.erase(s1);
}
}
for (auto i = m.rbegin(); i != m.rend(); i++)
{
cout << i->first << "\n";
}
}