-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_renewal.cpp
64 lines (54 loc) · 1.58 KB
/
menu_renewal.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
#include "menu_renewal.h"
#include <algorithm>
#include <unordered_map>
std::vector<std::string> GenerateCombination(const std::string& str, const int k)
{
std::vector<std::string> combinations;
const auto str_size = static_cast<int>(str.length());
if (k == 0)
{
combinations.emplace_back("");
return combinations;
}
for (auto i = 0; i < str_size; ++i)
{
auto suffix_combinations = GenerateCombination(str.substr(i + 1), k - 1);
for (const auto& suffix : suffix_combinations)
{
auto combination = str[i] + suffix;
combinations.push_back(combination);
}
}
return combinations;
}
std::vector<std::string> MenuRenewal(std::vector<std::string> orders, const std::vector<int>& course)
{
auto result = std::vector<std::string>{};
for (const auto count : course)
{
std::unordered_map<std::string, int> map;
auto popular = 1;
for (auto& order : orders)
{
std::sort(order.begin(), order.end());
auto combination = GenerateCombination(order, count);
for (const auto& item : combination)
{
++map[item];
if (map[item] > popular)
{
popular = map[item];
}
}
}
for (const auto& [key, value] : map)
{
if (popular > 1 && value == popular)
{
result.push_back(key);
}
}
}
std::sort(result.begin(), result.end());
return result;
}