๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš“ Self Study/๐Ÿ”“ Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ, 2019 KAKAO BLIND RECRUITMENT) C++

by UKHYUN22 2021. 12. 29.
728x90

 

 

 ๊ณต๋ฐฑ์ด ํฌํ•จ๋œ ๋ฌธ์ž์—ด์ธ stringstream์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์„ ์ฒ˜์Œ ๋ฐฐ์› ๋‹ค. ์ผ์ผ์ด if๋ฌธ์„ ํ†ตํ•ด์„œ substr๋กœ ๋น„๊ต๋ฅผ ํ–ˆ๊ณ  ์ด๋ฅผ ํ†ตํ•ด Test Case๋ฅผ ํ†ต๊ณผํ•œ ๋ฐฉ๋ฒ•์€ ๊ตฌํ˜„์„ ํ–ˆ์œผ๋‚˜ Time Problem์œผ๋กœ ์‹คํŒจํ–ˆ์—ˆ๋‹ค. Stringstream์„ ํ†ตํ•ด์„œ ๊ฐ„ํŽธํžˆ ์›ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ๋ฝ‘์•„๋‚ด๊ณ  If ๋ฌธ์„ ํ™œ์šฉํ•ด์„œ ์›ํ•˜๋Š” ๋…ผ๋ฆฌ๋กœ ๊ตฌํ˜„์‹œํ‚ฌ ์ˆ˜ ์žˆ์—ˆ๋‹ค. Map ์˜ ๊ฒฝ์šฐ๋„ insert(make_pair)์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ณ  find("์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž์—ด")->second ์ด๋ ‡๊ฒŒ ์„ ์–ธํ•ด์„œ ํ• ๋‹น์„ ํ–ˆ์—ˆ๋‹ค. ์ด์ œ๋Š” ์›ํ•˜๋Š” ๋ฌธ์ž์—ด์„ indexํ™” ์‹œ์ผœ์„œ ํ• ๋‹น์„ ํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™๋‹ค.

 ์‚ฌ์‹ค ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ตฌ๊ธ€์— ์กด์žฌํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ๊ทธ๋ž˜๋„ ๋ฐฐ์šด๊ฑด ๋ฐฐ์šด ๊ฑฐ๋‹ˆ...

 

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <map>

using namespace std;

vector<string> solution(vector<string> record) {
    vector<string> answer;
    map<string,string> map;
    vector<string> sent;
    
    // record์˜ ํฌ๊ธฐ๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.
    for(int i =0 ; i < record.size() ; i++) {
        string str[3];
        string token;
        
        // ๊ณต๋ฐฑ์ด ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•œ stringstream ์„ ์–ธ
        stringstream ss(record[i]);
        int count = 0;
        
        // ๋ฌธ์ž์—ด ss์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์„ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ token(string)์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์ด ์กด์žฌํ•  ๊ฒฝ์šฐ
        // str ๋ฐฐ์—ด์— ํ•ด๋‹นํ•˜๋Š” token์„ ๋‹ด๋Š”๋‹ค.
        while(ss >> token) {
            str[count++] = token;
        }
        
        // ๋งŒ์•ฝ ์ฒ˜์Œ ๋“ค์–ด์˜ค๋Š” ๋‹จ์–ด๊ฐ€ Enter์ธ ๊ฒฝ์šฐ
        if(str[0] == "Enter") {
            // sent ๋ฒกํ„ฐ์—๋Š” ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ push_back์„ ํ•ด์„œ ๋Œ€๊ธฐ์‹œ์ผœ ๋†“๋Š”๋‹ค.
            sent.push_back("๋‹˜์ด ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค.");
            
            // answer์—๋Š” ์ด๋ฆ„์ด ์•„๋‹Œ id๋ฅผ ๋จผ์ € ์ €์žฅ์‹œํ‚จ๋‹ค.
            answer.push_back(str[1]);
            
            // map์—์„œ id์— ํ•ด๋‹นํ•˜๋Š” username์„ ์ €์žฅ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•
            map[str[1]] = str[2];
        }
        else if(str[0] == "Leave") {
            // Leave์˜ ๊ฒฝ์šฐ๋„ sent ๋ฒกํ„ฐ์— push_back
            sent.push_back( "๋‹˜์ด ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.");
            
            // answer์— id๋ฅผ ์ €์žฅ์‹œํ‚จ๋‹ค.
            answer.push_back(str[1]);
        }
        else {
            // Change์˜ ๊ฒฝ์šฐ ํ•ด๋‹นํ•˜๋Š” id์— ๋ณ€๊ฒฝ๋œ ์ด๋ฆ„์„ ํ• ๋‹นํ•œ๋‹ค.
            map[str[1]] = str[2];
        }
    }
    
    for(int i =0 ; i < answer.size() ; i++) {
        // answer์— ๋ˆ„์ ๋œ id์— ํ•ด๋‹นํ•˜๋Š” username์„ map์„ ์ด์šฉํ•ด์„œ ์น˜ํ™˜์„ ํ•ด์ค€๋‹ค.
        answer[i] = map[answer[i]] + sent[i];
    }
    
    return answer;
}

 

 

StringStream์•Œ๊ธฐ ์ด์ „ substr๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ–ˆ์„ ๋•Œ

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>

using namespace std;

vector<string> solution(vector<string> record) {
    vector<string> answer;
    vector<int> space(2,0);
    unordered_map<string,string> map;
    int count = 0 ;
    
    for(int i = 0 ; i < record.size() ; i++) {
        for(int j = 0 ; j < record[i].size() ; j++) {
            if(record[i][j] == ' ') space[count++] = j;
            
        }
        if(record[i][0] == 'E') {
            string id = record[i].substr(space[0]+1, space[1]-space[0]-1);
            string name = record[i].substr(space[1]+1, record[i].size()-space[1]-1);
            if(map.find(id) != map.end())
                map.find(id)->second = name;
            else map.insert(make_pair(id, name));
            answer.push_back(id + "๋‹˜์ด ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค.");
         } else if(record[i][0] == 'L') {
            string id = record[i].substr(space[0]+1, record[i].size()-space[0]-1);
            answer.push_back(id + "๋‹˜์ด ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.");
         } else if(record[i][0] == 'C') {
            string id = record[i].substr(space[0]+1, space[1]-space[0]-1);
            string name = record[i].substr(space[1]+1, record[i].size()-space[1]-1);
            map.find(id)->second = name;
         }
        count = space[0] = space[1] = 0;
    }

    for(int i =0 ; i < answer.size() ; i++) {
        for(int j =0 ; j < answer[i].size() ; j++) {
            if(answer[i][j] == ' ') {
                string userid = answer[i].substr(0, j-6);
                answer[i].replace(0,j-6, map.find(userid)->second);
            }
        }
    }
    
    return answer;
}