Algorithm/백준

[백준] 7785 회사에 있는 사람 with C++

nowkoes 2023. 4. 18. 00:00

문제설명


입출력 예제


개념

 key와 value로 이루어진 map 자료구조를 사용하면 쉽게 해결할 수 있는 문제다. leave가 되어 있는 사원은 map에서 제거하고, enter만 되어있는 사원만 출력하면 해결할 수 있다. 마지막에 사전의 역순으로 출력하라고 되어 있음에 유의하자.


풀이

#include <iostream>
#include <algorithm>
#include <map>

int main()
{
	std::map<std::string, std::string> m;
	int N;
	std::cin >> N;

 사원의 이름과 상태가 문자열로 되어 있으므로 key와 value 모두 문자열로 받는 맵을 초기화 한다. 그리고 로그 수 N을 초기화한다.

	while (N--)
	{
		std::string name, log;
		std::cin >> name >> log;
		m[name] = log;
	}

 이름과 로그를 받고, 맵에 넣는다.

 

	for (auto it = m.rbegin(); it != m.rend(); ++it)
	{
		if (it->second == "enter")
			std::cout << it->first << '\n';
	}
}

 사전의 역순이므로 반복자를 rbegin()부터 rend()까지 순회시켜주고, key값을 출력하면 해결할 수 있다.

 

총합본

#include <iostream>
#include <algorithm>
#include <map>

int main()
{
	std::map<std::string, std::string> m;
	int N;
	std::cin >> N;

	while (N--)
	{
		std::string name, log;
		std::cin >> name >> log;
		m[name] = log;
	}

	for (auto it = m.rbegin(); it != m.rend(); ++it)
	{
		if (it->second == "enter")
			std::cout << it->first << '\n';
	}
}

 

반응형