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
64
65
66
67
68
69
#include <vector>
#include <string>
#include <sstream>
#include <map>
 
using namespace std;
 
//입력받은 문자열을 delimiter로 나누어 분할시킨다.
//ex) i am a boy => string str[4] = { "i","am","a","boy"};
vector<string> split(string input, char delimiter) {
    vector<string> answer;
    stringstream ss(input);
    string temp;
 
    while (getline(ss, temp, delimiter)) {
        answer.push_back(temp);
    }
 
    return answer;
}
 
vector<string> solution(vector<string> record) {
    vector<string> answer;
 
    //uid와 그에 따른 닉네임을 저장하는 맵
    map<stringstring> nickNameMap;
 
    for (int i = 0; i < record.size(); i++)
    {
        vector<string> words = split(record[i], ' ');
 
        string behavior = words[0];
        string uid = words[1];
        
        //들어오거나 , 닉네임을 바꿀 경우, 맵을 갱신한다.
        if (behavior == "Change" || behavior == "Enter")
        {
            string nickName = words[2];
 
            //이미 해당 uid에 할당된 닉네임이 있을 경우
            if (nickNameMap.find(uid) != nickNameMap.end())
            {
                nickNameMap[uid] = nickName;
            }
            //없을 경우
            else
            {
                nickNameMap.insert({ uid, nickName });
            }
        }
    }
 
    for (int i = 0; i < record.size(); i++)
    {
        vector<string> words = split(record[i], ' ');
 
        string behavior = words[0];
        string uid = words[1];
 
        if (behavior == "Change")
            continue;
 
        //맵에서 uid로 찾은 닉네임과 조합해서 메시지를 만든다.
        string notice = nickNameMap[uid] + (behavior == "Enter" ? "님이 들어왔습니다." : "님이 나갔습니다.");
        answer.push_back(notice);
    }
 
    return answer;
}
cs

+ Recent posts