[프로그래머스] 메뉴 리뉴얼 C++

semi·2022년 6월 28일
0

coding test

목록 보기
17/57

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

#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;


int checked[26] = { 0 };
int N = 26;

map<vector<string>, int> all_v;
vector<string> v;

void DFS(int c, int cur_i, int cnt, vector<string> order)
{
	if (cnt == c)
	{
		all_v[v] += 1;
		return;
	}
	for (int i = cur_i; i < order.size(); i++)
	{
		if (checked[i] == 0)
		{
			checked[i] = 1;
			cnt++;
			v.push_back(order[i]);
			DFS(c, i, cnt, order);
			checked[i] = 0;
			cnt--;
			v.pop_back();
		}
	}
}

vector<string> solution(vector<string> orders, vector<int> course)
{
	vector<string> answer;
	vector<map<vector<string>, int>> course_permutation;
	vector<vector<string>> order_vec;
	for (int i = 0; i < orders.size(); i++)
	{
		vector<string> tmp;
		for (int j = 0; j < orders[i].size(); j++)
		{
			string str = "";
			str += orders[i][j];
			tmp.push_back(str);
		}
		sort(tmp.begin(), tmp.end());
		order_vec.push_back(tmp);
	}
	for (int i = 0; i < course.size(); i++)
	{
		all_v.clear();

		for (auto order : order_vec)
		{
			DFS(course[i], 0, 0, order);
		}
		course_permutation.push_back(all_v);

	}
	
	vector<priority_queue<int>> num_cnt;

	for (auto x : course_permutation)
	{
		priority_queue<int> pq;
		for (auto y : x)
		{
			pq.push(y.second);
		}
		int max_val = 0;
		if (!pq.empty())
		{
			max_val = pq.top();
		}
		for (auto y : x)
		{
			if (max_val >= 2 && y.second == max_val)
			{
				string ans = "";
				for (auto z : y.first)
				{
					ans += z;
				}
				answer.push_back(ans);
			}
		}
	}
	sort(answer.begin(), answer.end());
	return answer;
}

int main(void)
{
	vector<string> orders1 = { "ABCFG", "AC", "CDE", "ACDE", "BCFG", "ACDEH" };
	vector<string> orders2 = { "ABCDE", "AB", "CD", "ADE", "XYZ", "XYZ", "ACD" };
	vector<string> orders3 = { "XYZ", "XWY", "WXA" };
	vector<int> course1 = { 2, 3, 4 }, course2 = { 2, 3, 5 }, course3 = { 2, 3, 4 };
	vector<string> answer = solution(orders3, course3);
	for (auto x : answer)
	{
		cout << x << " ";
	}
	return 0;
}

0개의 댓글