문제 : https://programmers.co.kr/learn/courses/30/lessons/42888
카카오 2019 BLINE RECRUITMENT '오픈 채팅방'
맵 Map
자료구조 Map을 이용해서 문제를 해결할 수 있습니다. 실제 UserID 와 NickName을 자료구조 Map에 담아두고, NickName이 바뀌면 Map을 갱신하고, Enter 혹은 Leave 명령어가 들어오면 NickName을 신경쓰지말고, UserID를 기준으로 Enter / Leave 명령 상태를 차례대로 저장해줍니다. 그 후, 저장해놓은 명령 상태를 Map에서 UserID에 해당하는 NickName을 이용해 제대로 원하는 답을 출력해주면 됩니다.
#include <string>
#include <vector>
#include<map>
using namespace std;
vector<string> solution(vector<string> record) {
int index = 0;
map<string, string> id;
vector<pair<string, bool>> tempAns;
for (int i = 0; i < record.size(); i++) {
int j;
string tempID = "", tempNick = "";
if (record[i][0] == 'E') { // Enter
j = 6;
while (record[i][j] != ' ')
tempID += record[i][j++];
j++;
while (record[i][j] != '\0')
tempNick += record[i][j++];
if (id.count(tempID) == 0)
id.insert({ tempID, tempNick });
else
id[tempID] = tempNick;
tempAns.push_back({ tempID, 0 });
}
else if (record[i][0] == 'L') { // Leave
j = 6;
while (record[i][j] != '\0')
tempID += record[i][j++];
tempAns.push_back({ tempID, 1 });
}
else { // Change
j = 7;
while (record[i][j] != ' ')
tempID += record[i][j++];
j++;
while (record[i][j] != '\0')
tempNick += record[i][j++];
id[tempID] = tempNick;
}
}
vector<string> answer;
for (int i = 0; i < tempAns.size(); i++) {
string temp;
if (tempAns[i].second == 0)
temp = id[tempAns[i].first] + "님이 들어왔습니다.";
else
temp = id[tempAns[i].first] + "님이 나갔습니다.";
answer.push_back(temp);
}
return answer;
}
'Problem Solving > Programmers' 카테고리의 다른 글
[Programmers] 뉴스 클러스터링 (0) | 2020.09.11 |
---|---|
[Programmers] 후보키 (0) | 2020.09.09 |
[Programmers] 외벽 점검 (0) | 2020.04.27 |
[Programmers] 기둥과 보 설치 (0) | 2020.04.24 |
[Programmers] 가사 검색 - travelbeeee (0) | 2020.04.23 |