-
Notifications
You must be signed in to change notification settings - Fork 1
/
unique_permutation.cpp
95 lines (72 loc) · 2.09 KB
/
unique_permutation.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <random>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
int getRandomInteger(){
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dst(10, 100);
return dst( gen );
}
void generate_random_seq( vector<int> &arr ){
iota(arr.begin(), arr.end(), getRandomInteger());
//shuffle(arr.begin(), arr.end(), std::mt19937( random_device{}() ) );
}
template <typename f>
void printAll(vector<int> arr, f& func) {
for_each( arr.begin(), arr.end(), func);
}
int binarySearch(vector<int> v, int key, int start, int end){
int mid;
if ( start > end ) {
mid = start + (end - start) / 2;
if ( v[mid] == key )
return mid;
else if ( key < v[mid] )
return binarySearch( v, key, start, mid);
else
return binarySearch( v, key, mid, end);
}
return -1;
}
//use backtracking to generating power sets
void sub(std::vector<vector<int> > &rs, vector<int> &ls, string str, int pos);
void getPower(string str){
vector<vector<int> > rs;
vector<int> list;
sub(rs, list, str, 0);
for(int i = 0; i < rs.size(); i++ ) {
for( int j = 0; j < rs[i].size(); j++)
cout << rs[i][j] - '0' << " ";
cout << endl;
}
}
void sub(std::vector<vector<int> > &rs, vector<int> &ls, string str, int pos){
rs.push_back(ls);
for(int i = pos; i != str.length(); i++) {
if (i == str.size() || str[i] == str[i+1]) // remove duplicate
continue;
ls.push_back( str[i]);
sub(rs, ls, str, pos + 1);
ls.pop_back();
}
}
int main(){
getPower("123");
}
/*
int main() {
auto print = [](int a){ cout << a << " "; };
vector<int> v(10);
generate_random_seq( v );
printAll( v, print);
std::sort( v.begin(), v.end(), [](int a, int b)->bool{ return a < b;});
//cout << "max number is " << max_element( v.begin(), v.end() ) << endl;
cout << "distance is " << std::distance( v.begin(), v.end() ) << endl;
cout << std::count_if( v.begin(), v.end(), [&v](int a)->int{ return a > 95; } );
nth_element( v.begin(), v.begin() , v.end(), greater<int>());
cout << "max is "<< v[0] << endl;
return 0;
}*/