Skip to content

Commit c50ad83

Browse files
committed
add perm (permutation)
1 parent 937230b commit c50ad83

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

perm.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Permutation
2+
// https://youtu.be/-j02o6__jgs?t=7302
3+
struct Perm : vector<int> {
4+
#define n (int)(size())
5+
#define p (*this)
6+
Perm(int _n): vector<int>(_n) {
7+
iota(begin(), end(), 0);
8+
}
9+
template<class...Args> Perm(Args...args): vector<int>(args...) {}
10+
Perm(initializer_list<int> a): vector<int>(a.begin(),a.end()) {}
11+
Perm operator+(const Perm& a) const {
12+
Perm r(n);
13+
for (int i = 0; i < n; ++i) r[i] = p[a[i]];
14+
return r;
15+
}
16+
Perm& operator+=(const Perm& a) {
17+
return *this = (*this)+a;
18+
}
19+
Perm operator-() const {
20+
Perm r(n);
21+
for (int i = 0; i < n; ++i) r[p[i]] = i;
22+
return r;
23+
}
24+
Perm operator-(const Perm& a) const {
25+
return *this + -a;
26+
}
27+
Perm& operator-=(const Perm& a) {
28+
return *this += -a;
29+
}
30+
// next permutation
31+
bool operator++() {
32+
return next_permutation(begin(),end());
33+
}
34+
#undef n
35+
#undef p
36+
};

0 commit comments

Comments
 (0)