ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [2019 카카오 신입 공채 1차 코딩테스트] 1. 오픈채팅방
    알고리즘/etc 2019. 7. 13. 21:39
    반응형

    자세한 문제 정보 및 풀이는 공식 사이트인 여기서 -> http://tech.kakao.com/2018/09/21/kakao-blind-recruitment-for2019-round-1/

     

    2019 카카오 신입 공채 1차 코딩 테스트 문제 해설

    작년에 이어 올해도 블라인드 전형으로 카카오 개발 신입 공채가 시작되었습니다! 그 첫 번째 관문으로 1차 온라인 코딩 테스트가 지난 9월 15일(토) 오후 2시부터 7시까지 5시간 동안 치러졌는데요. 지원자분들 만큼이나 준비위원들도 테스트가 문제없이, 공정하게 치러질 수 있도록 많은 준비를 했고 두근 거리는 마음으로 끝까지 온라인 테스트를 모니터링했답니다. 문제는 작년과 비슷하게 구현 문제 위주로 쉬운 난이도에서 어려운 난이도 순으로 풀 수 있도록 차례대

    tech.kakao.com

     

    문제를 읽고 단순 구현 문제라는 생각이 들었다. 

    결국 핵심은 uid에 해당하는 최종 닉네임이 뭔지를 매칭시키는 것이라고 생각해서 map을 사용했다!

     

    먼저 record(명령어)를 cmd, uid, nickname으로 분리한다. 이 때 split함수를 쓰려고 했는데 C++에 없다는 것을 발견... Java에서는 아무 생각없이 썼던것같은데. 아무튼 그래서 호다닥 구현해주고

    (Leave명령어 뒤에는 닉네임이 안들어온다는걸 깜빡하고 생각없이 구현했다. 자꾸 index 에러나서 뭐지?? 하다가 발견 ..문제잘읽기 ㅠㅠ)

     

    이제 cmd를 보면서 uid와 nickname을 map을 이용해서 매핑시켜주면 된다.  Enter명령어가 처음들어왔을 경우에는 무조건 map에 insert, 만약 map에서 찾았는데 uid에 매핑된 닉네임이 있을 경우에는 erase하고 덮어써주면된다!

    간단쓰~

     

    매핑까지끝났으면 record 하나하나 읽으면서 해당 명령어에 맞게 msg를 만들어서 answer에 푸쉬해주면 된다!

     

    소스코드 보기

    (코드 개판 주의)

    ...더보기

     

    (

    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
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    #include <string>
    #include <vector>
    #include <map>
    #include <iostream>
     
    using namespace std;
     
    vector<string> solution(vector<string> record) {
        vector<string> answer;
        vector<string> cmd;
        vector<string> uid;
        vector<string> nickName;
        map<stringstring> m;
        for (int i = 0; i < record.size(); i++)
        {
            string temp = "";
            int count = 0;
            for (int j = 0; j < record[i].size(); j++)
            {
                if (record[i][j] == ' ')
                {
                    if (count == 0)
                    {
                        cmd.push_back(temp);
                        count++;
                        temp = "";
                    }
                    else if (count == 1)
                    {
                        uid.push_back(temp);
                        count++;
                        temp = "";
                    }
                }
                else
                    temp += record[i][j];
            }
            if (count == 1)
            {
                uid.push_back(temp);
                nickName.push_back("djkldjlk");
            }
            else
                nickName.push_back(temp);
     
        }//split records into answer, cmd, uid
        //m.insert({ "","" });
        for (int i = 0; i < record.size(); i++)
        {
            if (cmd[i] == "Enter")
            {
                if (m.find(uid[i]) == m.end())
                {
                    //cout << "NO" << endl;
                    m.insert({ uid[i],nickName[i] });
     
                }
                //if it's first time entering, insert into map
                else
                {
                    m.erase(m.find(uid[i]));
                    m.insert({ uid[i],nickName[i] });
                }
                //else change nickname
     
            }
            else if (cmd[i] == "Change")
            {            
                m.erase(m.find(uid[i]));
                m.insert({ uid[i],nickName[i] });
            }
            else
                continue;
        }//mapping nicknames and uid with result
     
        for (int i = 0; i < record.size(); i++)
        {
            if (cmd[i] == "Enter")
            {
                string msg = m[uid[i]];
                msg += "님이 들어왔습니다.";
                answer.push_back(msg);
                //cout << msg << endl;
            }
            else if (cmd[i] == "Leave")
            {
                string msg = m[uid[i]];
                msg += "님이 나갔습니다.";
                answer.push_back(msg);
                //cout << msg << endl;
     
            }
            else
            {
                continue;
     
            }
     
        }
     
        return answer;
    }
    int main()
    {
        vector<string> ans;
        vector<string> record = { "Enter uid1234 Muzi""Enter uid4567 Prodo","Leave uid1234","Enter uid1234 Prodo","Change uid4567 Ryan" };
        ans = solution(record);
    }
    http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
    http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

     

     

     

    반응형

    댓글

Designed by Tistory.