문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/42888
코딩테스트 연습 - 오픈채팅방
오픈채팅방 카카오톡 오픈채팅방에서는 친구가 아닌 사람들과 대화를 할 수 있는데, 본래 닉네임이 아닌 가상의 닉네임을 사용하여 채팅방에 들어갈 수 있다. 신입사원인 김크루는 카카오톡 오
programmers.co.kr
문자열 처리와 함께 해시를 적절히 이용하면 쉽게 풀 수 있는 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include <map> | |
#include <iostream> | |
using namespace std; | |
typedef struct | |
{ | |
bool isEnter; | |
string uid; | |
} User; | |
vector<string> solution(vector<string> record) { | |
map<string, string> uid2nickname; | |
vector<User> chatHistories; | |
for (int idx = 0; idx < record.size(); idx++) | |
{ | |
string temp; | |
string uid; | |
int tempIdx = 0; | |
bool isEnter; | |
bool isChange = false; | |
for (char c : record[idx]) | |
{ | |
if (c == ' ') | |
{ | |
if (tempIdx == 0) | |
{ | |
isEnter = temp == "Enter"; | |
isChange = temp == "Change"; | |
} | |
else | |
{ | |
uid = temp; | |
} | |
tempIdx++; | |
temp = ""; | |
continue; | |
} | |
temp += c; | |
} | |
if (tempIdx == 1) | |
{ | |
uid = temp; | |
} | |
else | |
{ | |
uid2nickname[uid] = temp; | |
} | |
if (isChange == false) | |
{ | |
chatHistories.push_back({isEnter, uid}); | |
} | |
} | |
vector<string> answer; | |
for (User user : chatHistories) | |
{ | |
string s = uid2nickname[user.uid]; | |
s += "님이 "; | |
s += user.isEnter ? "들어왔습니다." : "나갔습니다."; | |
answer.push_back(s); | |
} | |
return answer; | |
} |

개발환경: Programmers IDE
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] [1차] 뉴스 클러스터링 (0) | 2022.01.30 |
---|---|
[Programmers] 괄호 변환 (0) | 2022.01.29 |
[Programmers] 카카오프렌즈 컬러링북 (0) | 2022.01.26 |
[Programmers] 단체사진 찍기 (0) | 2022.01.26 |
[Programmers] k진수에서 소수 개수 구하기 (0) | 2022.01.19 |