

map 자료구조를 이용한 간단한 구현문제였다. uid별로 사용자를 구분하고, 각 uid에 해당하는 nickname을 미리 map에 저장 해놓는다.
이 후 이러한 입장/퇴장 정보를 저장해놓은 info 구조체를 원소로 하는 vector를 순회하며 정보를 출력하면 된다.
코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#include <string>
#include <vector>
#include <map>
using namespace std;
typedef struct info
{
string uid;
string text;
}info;
map<string, string> m;
vector<string> solution(vector<string> record) {
vector<string> answer;
vector<info> inf;
for(int i=0; i<record.size(); i++)
{
string tmp = "";
vector<string> command;
for(int j=0; j<record[i].size(); j++)
{
if (record[i][j] == ' ')
{
command.push_back(tmp);
tmp = "";
}
else
tmp += record[i][j];
}
command.push_back(tmp);
info info_tmp;
if (command[0] == "Enter")
{
m[command[1]] = command[2];
info_tmp.uid = command[1];
info_tmp.text = "님이 들어왔습니다.";
inf.push_back(info_tmp);
}
else if (command[0] == "Leave")
{
info_tmp.text = "님이 나갔습니다.";
info_tmp.uid = command[1];
inf.push_back(info_tmp);
}
else if (command[0] == "Change")
m[command[1]] = command[2];
}
for(int i=0; i<inf.size(); i++)
{
info info_tmp = inf[i];
string res = m[info_tmp.uid] + info_tmp.text;
answer.push_back(res);
}
return answer;
}
|
cs |
'Study > Programmers' 카테고리의 다른 글
프로그래머스 - 단체사진 찍기(2017 카카오코드 본선) (0) | 2021.05.04 |
---|---|
프로그래머스 - 키패드 누르기(2020 카카오 인턴십) (0) | 2021.05.04 |
프로그래머스 - 징검다리 건너기(2019 카카오 개발자 겨울 인턴십) (0) | 2021.05.01 |
프로그래머스 - 자물쇠와 열쇠(2020 KAKAO BLIND RECRUITMENT) (0) | 2021.04.29 |
프로그래머스 - 괄호 변환 (0) | 2021.04.22 |