-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermuteSingle.cpp
More file actions
68 lines (55 loc) · 1.47 KB
/
Copy pathPermuteSingle.cpp
File metadata and controls
68 lines (55 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
67
68
/*
Program to calculate all of the possible permutations of a heterogram, single-thread
*/
#include <algorithm>
#include <vector>
#include <iostream>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <string>
#include <unordered_set>
using Clock = std::chrono::high_resolution_clock;
const unsigned int N = std::thread::hardware_concurrency(); //number of threads on device
std::vector<std::string> permutations;
//calculate factorial
long long int fact(int N)
{
if (N==0 || N==1)
{
return 1;
}
int total = 1;
for (int i=1; i<=N; i++)
{
total *= i;
}
return total;
}
// Driver Code
int main()
{
srand(time(0));
const int factor = 11;
std::string str;
str.reserve(factor);
permutations.reserve(fact(factor));
for (int i=65; i<65+factor; i++)
{
str.push_back(static_cast<char>(i));
}
std::cout << str << '\n';
auto start_time = Clock::now();
do {permutations.push_back(str);} while(std::next_permutation(str.begin(),str.end()));
auto end_time = Clock::now();
// std::cout << permutations.size() << '\n';
// for (auto& i: permutations)
// {
// std::cout << i << '\n';
// }
std::cout << "Time difference = "
<< std::chrono::duration<double, std::nano>(end_time - start_time).count() << " nanoseconds\n";
// std::unordered_set test(permutations.begin(),permutations.end());
// std::cout << test.size() << '\n';
}