-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicProgramming.cpp
More file actions
50 lines (45 loc) · 1.74 KB
/
Copy pathdynamicProgramming.cpp
File metadata and controls
50 lines (45 loc) · 1.74 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
//https://www.tutorialspoint.com/bitmasking-and-dynamic-programming-in-cplusplus
//
// Problem
//
//There are 50 caps with numbers from 1 to 50. The N people have some of these caps in their collection. One day all of them wear a cap to a party. But all need to look unique so, they decided to wear different numbered caps each. We are given the n number of people and the cap numbers in their collections. Our task is to find the total number of ways in which they can wear the cap so that everyone looks unique.
//In the problem, the first line contains value n i.e. the number of people. The next n lines contain their collection.
//
//Input:
//3
//4 45 10
//25
//45 10
//Output:
//4
//
//All possible ways are (4, 25, 45) , (4, 25, 10), (45, 25, 10), (10, 25, 45).
//
//The output should be in the form of 1000000007 as the number of ways can be a large number.
//
//To solve this problem, a simple solution is to find all possible combinations of people using caps. Starting from the first set and recur the remaining sets. But this solution is not optimized.
//
//A better solution is using Bitmasking and DP by creating a mask of size 210 for 10 persons. And a vector of caps of size 51. Then, we will recur in a number of ways.
#include <bits/stdc++.h>
#include <iterator>
#include <string>
using namespace std;
int solve() {
std::vector<std::vector<int>> nums;
std::string line;
int n;
cin >> n;
std::getline(std::cin, line);
while (std::getline(std::cin, line)) {
std::istringstream ss(line);
nums.emplace_back(std::istream_iterator<int>{ss}, std::istream_iterator<int>{});
}
cout <<nums[0].size() << "\n";
cout << nums[0][0] << "\n";
return 0;
}
int main (int argc, char *argv[])
{
solve();
return 0;
}