#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
vector<string> solution(vector<string> orders, vector<int> course) {
vector<string> answer;
map<string, int> result;
for (int i = 0; i < orders.size(); i++) {
sort(orders[i].begin(), orders[i].end());
}
for (int i = 0; i < course.size(); i++) {
int course_size = course[i];
for (int j = 0; j < orders.size(); j++) {
int order_size = orders[j].size();
if (course_size <= order_size){
vector<bool> visit(order_size - course_size, false);
visit.insert(visit.end(), course_size, true);
do {
string tmp = "";
for (int k = 0; k < order_size; k++) {
if (visit[k]) {
tmp = tmp + orders[j][k];
}
}
// tmp 가 존재하면
if (result.count(tmp)) {
result[tmp] ++;
}
else {
result[tmp] = 1;
}
} while (next_permutation(visit.begin(), visit.end()));
}
}
}
// 1개인거 지우기
for (auto iter = result.begin(); iter != result.end();) {
if (iter->second <= 1) {
iter = result.erase(iter);
}
else {
iter++;
}
}
// 가장 많이 주문된거 찾기
for (int i = 0; i < course.size(); i++) {
int course_count = course[i];
vector<string> tmp;
int max = 0;
for (auto iter = result.begin(); iter != result.end(); iter++) {
if (iter->first.size() == course_count) {
//최대값 갱신
if (max < iter->second) {
max = iter->second;
if (tmp.size() > 0) {
tmp.clear();
tmp.push_back(iter->first);
}
else {
tmp.push_back(iter->first);
}
}
//최대값이랑 같을때
else if (max == iter->second) {
tmp.push_back(iter->first);
}
}
}
for (int j = 0; j < tmp.size(); j++) {
answer.push_back(tmp[j]);
}
}
sort(answer.begin(), answer.end());
return answer;
}