끄적끄적 코딩
article thumbnail

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;
}

검색 태그