Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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
Tags
more
Archives
Today
Total
관리 메뉴

Study

프로그래머스-메뉴리뉴얼 본문

알고리즘/C++ 문제풀이

프로그래머스-메뉴리뉴얼

^_^? 2021. 7. 28. 11:26
 

코딩테스트 연습 - 메뉴 리뉴얼

레스토랑을 운영하던 스카피는 코로나19로 인한 불경기를 극복하고자 메뉴를 새로 구성하려고 고민하고 있습니다. 기존에는 단품으로만 제공하던 메뉴를 조합해서 코스요리 형태로 재구성해서

programmers.co.kr

 

<풀이>

더보기

#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

bool cmp(pair<string, int>a, pair<string, int> b){
    return a.second > b.second;
}


void DFS(map<string, int>& dic, string& order, string tmp, int index, int depth){
    if(depth == tmp.length()){
        dic[tmp]++;
        return;
    }

    for(int i = index; i<order.length(); i++){
        tmp[depth] = order[i];
        DFS(dic,order,tmp,i+1,depth+1);
    }
}

vector<string> solution(vector<string> orders, vector<int> course) {
    vector<string> answer;
    map<string, int> dic;
    
    for(int i=0; i<orders.size(); i++){
        sort(orders[i].begin(), orders[i].end());
        for(int j=0; j<course.size(); j++){
            string tmp = "";
            tmp.resize(course[j]);
            DFS(dic,orders[i], tmp,0,0);
        }
    }
   
    vector<pair<string,int>> sorted;
    
    for(auto& order : dic)//dic에서 차례대로 객체를 꺼내서 order에 넣는다
        if(order.second > 1)
            sorted.push_back({order.first,order.second});
    
    sort(sorted.begin(), sorted.end(),cmp);
    
    for(int i=0; i<course.size(); i++){
        int max = 0;
        for(int j=0; j<sorted.size(); j++){
            if(sorted[j].first.length() != course[i])
                continue;
            else if(max == 0){
                answer.push_back(sorted[j].first);
                max = sorted[j].second;
            }
            else if (max == sorted[j].second)
                answer.push_back(sorted[j].first);
            else
                break;
        }
    
    }
    
    sort(answer.begin(), answer.end());
    return answer;
}

 

-참고한 사이트

https://ansohxxn.github.io/programmers/82/