

주문한 메뉴(string)에 대해 course 메뉴의 개수만큼(2, 3, 4 ...) substring을 구하여 vector에 저장한 뒤, 가장 많이 주문한 코스 메뉴 조합을 answer에 추가하여 return하면 된다.
코드
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
|
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
map<string, int> m;
vector<string> v;
vector<string> solution(vector<string> orders, vector<int> course) {
vector<int> pick;
vector<string> answer;
int c_len = course.size();
int o_len = orders.size();
for(int j=0; j<o_len; j++)
sort(orders[j].begin(), orders[j].end());
for(int i=0; i<c_len; i++)
{
for(int j=0; j<o_len; j++)
{
int s_len = orders[j].size();
if (s_len < course[i])
continue;
for(int k=0; k<s_len - course[i]; k++)
pick.push_back(0);
for(int k=0; k<course[i]; k++)
pick.push_back(1);
do{
string tmp = "";
for(int k=0; k<pick.size(); k++)
if (pick[k] == 1)
tmp += orders[j][k];
if (!m[tmp])
v.push_back(tmp);
m[tmp]++;
}while (next_permutation(pick.begin(), pick.end()));
pick.clear();
}
int cnt = -1;
for(int j=0; j<v.size(); j++)
cnt = max(cnt, m[v[j]]);
if (cnt >= 2)
{
for(int j=0; j<v.size(); j++)
if (cnt == m[v[j]])
answer.push_back(v[j]);
}
v.clear();
m.clear();
}
sort(answer.begin(), answer.end());
return answer;
}
|
cs |
map과 next_permutation을 이용하여 해결했다.
'Study > Programmers' 카테고리의 다른 글
프로그래머스 - 괄호 변환 (0) | 2021.04.22 |
---|---|
프로그래머스 - 문자열 압축(2020 KAKAO BLIND RECRUITMENT) (0) | 2021.04.19 |
프로그래머스 - 신규 아이디 추천(2021 KAKAO BLIND RECRUITMENT) (0) | 2021.04.16 |
프로그래머스 - 합승 택시 요금(2021 KAKAO BLIND RECRUITMENT) (0) | 2021.04.15 |
프로그래머스 - 순위 (0) | 2021.04.15 |