FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm-examples/cpp-algorithm/src/math/permutation.cpp at main · codejsha/algorithm-examples · GitHub
codejsha
/
algorithm-examples
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm-examples
/
cpp-algorithm
/
src
/
math
/
permutation.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
101 lines (85 loc) · 2.78 KB
Breadcrumbs
algorithm-examples
/
cpp-algorithm
/
src
/
math
/
permutation.cpp
Copy path
File metadata and controls
101 lines (85 loc) · 2.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
permutation.h
"
#
include
<
algorithm
>
#
include
<
iostream
>
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]);
}
}
auto
Permutation::ApplyPermutationWithAdditionalSpace
(
const
std::vector<
int
>& permutation,
const
std::vector<
char
>& arr)
-> std::vector<char>
{
std::vector<
char
>
result
(arr.
size
());
for
(
int
i =
0
; i <
static_cast
<
int
>(arr.
size
()); ++i)
{
result[permutation[i]] = arr[i];
}
return
result;
}
auto
Permutation::ApplyPermutationBySwap
(std::vector<
int
>& permutation, std::vector<
char
>& arr) -> std::vector<char>
{
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;
}
auto
Permutation::InversePermutation
(
const
std::vector<
int
>& permutation,
const
std::vector<
char
>& arr)
-> std::vector<int>
{
std::vector<
int
>
result
(arr.
size
());
for
(
int
i =
0
; i <
static_cast
<
int
>(arr.
size
()); ++i)
{
result[permutation[i]] = i;
}
return
result;
}
auto
Permutation::NextPermutation
(std::vector<
int
>& permutation) -> std::vector<int>
{
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;
}
auto
Permutation::PreviousPermutation
(std::vector<
int
>& permutation) -> std::vector<int>
{
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;
}
auto
Permutation::KthPermutation
(std::vector<
int
>& permutation,
int
k) -> std::vector<int>
{
std::sort
(permutation.
begin
(), permutation.
end
());
do
{
if
(--k ==
0
)
{
return
permutation;
}
}
while
(
std::next_permutation
(permutation.
begin
(), permutation.
end
()));
return
{};
}
Back
|
FazBrowse Home
|
New Git URL