-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.h
More file actions
195 lines (171 loc) · 5.24 KB
/
Copy pathpermutation.h
File metadata and controls
195 lines (171 loc) · 5.24 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifndef CPP_ALGORITHM_PERMUTATION_H
#define CPP_ALGORITHM_PERMUTATION_H
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
namespace Permutation
{
/**
* \brief Permutation from a given string.
* \param str input string
* \param prefix prefix
*/
void Permutation(
const std::string& str,
const std::string& prefix);
/**
* \brief Permute the elements of an array.
* \param permutation permutation array
* \param arr array
* \return result array
*/
std::vector<char> ApplyPermutationWithAdditionalSpace(
const std::vector<int>& permutation,
const std::vector<char>& arr);
/**
* \brief Permute the elements of an array. (in-place)
* \param permutation permutation array
* \param arr array
* \return result array
*/
std::vector<char> ApplyPermutationBySwap(
std::vector<int>& permutation,
std::vector<char>& arr);
/**
* \brief Inverse permutation.
* \param permutation permutation array
* \param arr array
* \return inverse array
*/
std::vector<int> InversePermutation(
const std::vector<int>& permutation,
const std::vector<char>& arr);
/**
* \brief Compute the next permutation.
* \param permutation array
* \return next permutation array
*/
std::vector<int> NextPermutation(
std::vector<int>& permutation);
/**
* \brief Compute the previous permutation.
* \param permutation array
* \return previous permutation array
*/
std::vector<int> PreviousPermutation(
std::vector<int>& permutation);
/**
* \brief Compute the k-th permutation.
* \param permutation array
* \param k k-th index
* \return k-th permutation array
*/
std::vector<int> KthPermutation(
std::vector<int>& permutation,
int k);
}
// ----------------------------------------------------------------------------
inline void Permutation::Permutation(
const std::string& str,
const std::string& prefix)
{
const int size = static_cast<int>(str.length());
if (size == 0)
{
std::cout << prefix << std::endl;
return;
}
for (int i = 0; i < size; ++i)
{
const std::string rem = str.substr(0, i) + str.substr(i + 1);
Permutation(rem, prefix + str[i]);
}
}
// ----------------------------------------------------------------------------
inline std::vector<char> Permutation::ApplyPermutationWithAdditionalSpace(
const std::vector<int>& permutation,
const std::vector<char>& arr)
{
std::vector<char> result(arr.size());
for (int i = 0; i < static_cast<int>(arr.size()); ++i)
{
result[permutation[i]] = arr[i];
}
return result;
}
// ----------------------------------------------------------------------------
inline std::vector<char> Permutation::ApplyPermutationBySwap(
std::vector<int>& permutation,
std::vector<char>& arr)
{
for (int i = 0; i < static_cast<int>(arr.size()); ++i)
{
while (permutation[i] != i)
{
std::swap(arr[i], arr[permutation[i]]);
std::swap(permutation[i], permutation[permutation[i]]);
}
}
return arr;
}
// ----------------------------------------------------------------------------
inline std::vector<int> Permutation::InversePermutation(
const std::vector<int>& permutation,
const std::vector<char>& arr)
{
std::vector<int> result(arr.size());
for (int i = 0; i < static_cast<int>(arr.size()); ++i)
{
result[permutation[i]] = i;
}
return result;
}
// ----------------------------------------------------------------------------
inline std::vector<int> Permutation::NextPermutation(
std::vector<int>& permutation)
{
const auto inversionPoint =
std::is_sorted_until(permutation.rbegin(), permutation.rend());
if (inversionPoint == permutation.rend())
{
return {};
}
const auto leastUpperBound =
std::upper_bound(permutation.rbegin(), inversionPoint, *inversionPoint);
std::iter_swap(inversionPoint, leastUpperBound);
std::reverse(permutation.rbegin(), inversionPoint);
return permutation;
}
// ----------------------------------------------------------------------------
inline std::vector<int> Permutation::PreviousPermutation(
std::vector<int>& permutation)
{
const auto inversionPoint =
std::is_sorted_until(permutation.rbegin(), permutation.rend(), std::greater<>());
if (inversionPoint == permutation.rend())
{
return {};
}
const auto leastUpperBound =
std::upper_bound(permutation.rbegin(), inversionPoint, *inversionPoint, std::greater<>());
std::iter_swap(inversionPoint, leastUpperBound);
std::reverse(permutation.rbegin(), inversionPoint);
return permutation;
}
// ----------------------------------------------------------------------------
inline std::vector<int> Permutation::KthPermutation(
std::vector<int>& permutation,
int k)
{
std::sort(permutation.begin(), permutation.end());
do
{
if (--k == 0)
{
return permutation;
}
} while (std::next_permutation(permutation.begin(), permutation.end()));
return {};
}
#endif