2019 KAKAO BLIND RECRUITMENT (2019 카카오 블라인드 채용 문제)
채팅방에 들어오고 나간 기록들을 출력하는 문제입니다.
사용자는 Enter, Leave, Change 3가지를 할 수 있습니다.
Enter는 "ABC님이 들어왔습니다." 를 출력합니다. 다른 닉네임으로 같은 방에 들어올 경우 채팅방 내의 해당 id의 닉네임이 전부 변경됩니다.
Leave는 "ABC님이 나갔습니다." 를 출력합니다.
Change는 ABC라는 닉네임을 변경할 수 있습니다. 이때 변경된 내용은 채팅방 전체에 변경됩니다.
1. 입력된 값들을 차례대로 확인
2. Enter나 Change의 경우 해당 id의 닉네임을 변경해주는 작업을 해줍니다.
3. 변경 된 닉네임으로 Enter과 Leave의 명령어에 맞게 해당값 출력
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
using namespace std;
vector<string> solution(vector<string> record)
{
vector<string> answer;
map<string, string> m;
string command;
string id;
string nickname;
for (string input : record)
{
stringstream ss(input);
ss >> command;
ss >> id;
if (command == "Enter" || command == "Change")
{
ss >> nickname;
m[id] = nickname;
}
}
for (string input : record)
{
stringstream ss(input);
ss >> command;
ss >> id;
if (command == "Enter")
{
nickname = (m.find(id)->second);
answer.push_back(nickname + "님이 들어왔습니다.");
}
else if (command == "Leave")
{
nickname = (m.find(id)->second);
answer.push_back(nickname + "님이 나갔습니다.");
}
}
return answer;
}
'알고리즘' 카테고리의 다른 글
[C++] 프로그래머스 - 프렌즈 4블록 (0) | 2021.05.14 |
---|---|
[C++] 프로그래머스 - 뉴스 클러스터링 (0) | 2021.05.14 |
[C++] 프로그래머스 - 다트 게임 (0) | 2021.05.04 |
[C++] 프로그래머스 - 비밀지도 (0) | 2021.04.30 |
[C++] 프로그래머스 - 메뉴 리뉴얼 (0) | 2021.04.17 |