본문 바로가기

Study/Programmers

프로그래머스 - 오픈채팅방(2019 KAKAO BLIND RECRUITMENT)

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<stringstring> 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