-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations
More file actions
47 lines (45 loc) · 874 Bytes
/
permutations
File metadata and controls
47 lines (45 loc) · 874 Bytes
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
vector<int> multiply(vector<int> p1, vector<int> p2){
int n = p1.size();
vector<int> p(n);
for (int i = 0; i < n; i++)
p[i] = p2[p1[i]];
return p;
}
vector<int> parse(string s, int n){
vector<int> p(n);
for (int i = 0; i < n; i++)
p[i] = i;
int first = -1, prev = -1;
for (int i = 0; i < s.size(); i++){
if (s[i] == '(')
first = -1;
if (s[i] != '(' && s[i] != ')'){
if (first == -1)
first = int(s[i] - '1');
else
p[prev] = int(s[i] - '1');
prev = int(s[i] - '1');
}
if (s[i] == ')')
p[prev] = first;
}
return p;
}
void print(vector<int> p){
int n = p.size();
vector<int> used(n, false);
for (int i = 0; i < n; i++){
if (!used[i] && p[i] != i){
cout << '(' << i + 1;
used[i] = true;
int v = p[i];
while (v != i){
used[v] = true;
cout << v + 1;
v = p[v];
}
cout << ')';
}
}
cout << endl;
}