https://www.welcomekakao.com/learn/courses/30/lessons/42888



문제를 보면, ID와 닉네임을 대응해서 최신값으로 유지해야 한다. 기본적으로 사람이 입장하면 map에 추가를 해주면 되겠고, 닉네임을 변경하는 것이면, map에서 key값인 id에 대응되는 value인 닉네임을 업데이트 해주면 된다.


주의할 것은, 나가는 기능을 수행했을 경우, map에서 그 id의 데이터를 삭제하면 안 된다는 것이다.


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
57
58
59
60
61
62
63
#include<vector>
#include<string>
#include<iostream>
#include<map>
using namespace std;
struct Rec {
    int cmd; //입장 0 퇴장 1 변경 2
    string id;
};
vector<Rec> rec;
map<stringstring> mp; //id, nick
 
vector<string> solution(vector<string> record) {
    vector<string> answer;
    for (int i = 0; i < record.size(); i++) {
        string cur = record[i];
        bool beenSpace = false;
        int firSpace, secSpace = 0, curCmd;
        if (cur[0== 'E') curCmd = 0//입장
        else if (cur[0== 'L') curCmd = 1//나감
        else if (cur[0== 'C') curCmd = 2//닉변
 
        for (int j = 0; j < cur.length(); j++) {
        
            if (cur[j] == ' ') {
                if (!beenSpace) {
                    beenSpace = true;
                    firSpace = j;
                }
                else secSpace = j;
            }
        }
        string curNick = cur.substr(secSpace + 1, cur.length() - secSpace - 1);
        
        string curId;
        if (curCmd == 0) { //입장
            curId = cur.substr(firSpace + 1, secSpace - firSpace - 1);
            mp[curId] = curNick;
        
            rec.push_back({ curCmd, curId });
        }
        else if (curCmd == 1) { //나감
            curId = cur.substr(firSpace + 1, cur.length() - firSpace - 1);
            rec.push_back({ curCmd, curId });
        }
        else { //닉변
            curId = cur.substr(firSpace + 1, secSpace - firSpace - 1);
            mp[curId] = curNick;
        }
    }
 
    for (int i = 0; i < rec.size(); i++) {
        string str = "";
        if (rec[i].cmd == 0) {
            str = mp[rec[i].id] + "님이 들어왔습니다.";
        }
        else if(rec[i].cmd == 1)
            str = mp[rec[i].id] + "님이 나갔습니다.";
        
        answer.push_back(str);
    }
    return answer;
}
cs


+ Recent posts