forked from jyx-fyh/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (52 loc) · 1.47 KB
/
main.cpp
File metadata and controls
66 lines (52 loc) · 1.47 KB
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
65
66
// 动态规划求解并输出所有LCS
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
struct Info {
vector<int> coins;
vector<int> zhangs;
Info(const vector<int>& c, const vector<int>& z) : coins(c), zhangs(z) {}
};
Info getInfo(const vector<int>& arr) {
unordered_map<int, int> counts;
for (int value : arr) {
if (counts.find(value) == counts.end()) {
counts[value] = 1;
} else {
counts[value]++;
}
}
int N = counts.size();
vector<int> coins(N);
vector<int> zhangs(N);
int index = 0;
for (const auto& entry : counts) {
coins[index] = entry.first;
zhangs[index++] = entry.second;
}
return Info(coins, zhangs);
}
int process(const vector<int>& coins, const vector<int>& zhangs, int index, int rest) {
if (index == coins.size()) {
return rest == 0 ? 1 : 0;
}
int ways = 0;
for (int zhang = 0; zhang * coins[index] <= rest && zhang <= zhangs[index]; zhang++) {
ways += process(coins, zhangs, index + 1, rest - (zhang * coins[index]));
}
return ways;
}
int coinsWay(const vector<int>& arr, int aim) {
if (arr.empty() || aim < 0) {
return 0;
}
Info info = getInfo(arr);
return process(info.coins, info.zhangs, 0, aim);
}
int main() {
vector<int> arr = {1,2,1,1,2,1,2,2,3,3,3,3,4,5,5,5,6,6,7,7,8,9};
int aim = 20;
cout << coinsWay(arr, aim) << endl;
return 0;
}