본문 바로가기

알고리즘 · 코딩

[프로그래머스] 직업군 추천하기

위클리 챌린지 > 4주차_직업군 추천하기

 

문제 링크

https://programmers.co.kr/learn/courses/30/lessons/84325

 

코딩테스트 연습 - 4주차_직업군 추천하기

개발자가 사용하는 언어와 언어 선호도를 입력하면 그에 맞는 직업군을 추천해주는 알고리즘을 개발하려고 합니다. 아래 표는 5개 직업군 별로 많이 사용하는 5개 언어에 직업군 언어 점수를 부

programmers.co.kr


C++ 풀이

#include <string>
#include <vector>
#include <unordered_map>
#include <sstream>
using namespace std;

#define CONTENTS 0
#define GAME 1
#define HARDWARE 2
#define PORTAL 3
#define SI 4

string solution(vector<string> table, vector<string> languages, vector<int> preference) {
    vector <unordered_map<string, int> > whole_table(5);
    for (int i = 0; i < table.size(); ++i) {
        istringstream ss(table[i]);
        string word; int score = 5;
        getline(ss, word, ' ');
        
        if (word == "CONTENTS") {
            while (getline(ss, word, ' ')) {
                whole_table[CONTENTS].insert({ word, score });
                --score;
            }
        }
        else if (word == "GAME") {
            while (getline(ss, word, ' ')) {
                whole_table[GAME].insert({ word, score });
                --score;
            }
        }
        else if (word == "HARDWARE") {
            while (getline(ss, word, ' ')) {
                whole_table[HARDWARE].insert({ word, score });
                --score;
            }
        }
        else if (word == "PORTAL") {
            while (getline(ss, word, ' ')) {
                whole_table[PORTAL].insert({ word, score });
                --score;
            }
        }
        else if (word == "SI") {
            while (getline(ss, word, ' ')) {
                whole_table[SI].insert({ word, score });
                --score;
            }
        }
    }
    vector <int> score(5, 0);
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < languages.size(); ++j) {
            if (whole_table[i].find(languages[j]) != whole_table[i].end()) {
                score[i] += preference[j] * (*(whole_table[i].find(languages[j]))).second;
            }
        } 
    }
    string answer = "CONTENTS";
    int max_score = score[0];
    for (int i = 1; i < 5; ++i) {
        if (score[i] > max_score) {
            if (i == 1)
                answer = "GAME";
            else if (i == 2)
                answer = "HARDWARE";
            else if (i == 3)
                answer = "PORTAL";
            else if (i == 4)
                answer = "SI";
            max_score = score[i];
        }
    }
    return answer;
}

stringstream을 사용하여 문자열을 공백 단위로 잘랐다.

 

 

LIST