-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclicPermutations.java
More file actions
179 lines (158 loc) · 4.72 KB
/
Copy pathCyclicPermutations.java
File metadata and controls
179 lines (158 loc) · 4.72 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* A console application that solves tasks on cyclic permutations (AKA Zykelschreibweise) for the DS exam.
* @author Mariia Bogatyreva
* @author Alex Pokras
*/
public class CyclicPermutations {
private static class Permutation {
private final int max;
final int[] p;
Permutation(int max) {
this.max = max;
p = new int[max + 1];
for (int i = 1; i <= max; i++) {
p[i] = i;
}
}
void insertParen(String parenString) {
String[] intStrings = parenString.split(",");
int[] paren = new int[intStrings.length];
for (int i = 0; i < paren.length; i++) {
paren[i] = Integer.parseInt(intStrings[i]);
}
for (int i = 0; i < paren.length - 1; i++) {
p[paren[i]] = paren[i + 1];
}
p[paren[paren.length - 1]] = paren[0];
}
int next(int i) {
return p[i];
}
}
/** Calculate g(f(i)) */
private static int gfi(Permutation f, Permutation g, int i) {
return g.next(f.next(i));
}
/** Calculate g º f */
private static String gAfterF(Permutation f, Permutation g) {
int max = f.max;
if (max != g.max) throw new IllegalArgumentException();
boolean[] seen = new boolean[max + 1];
String ret = "";
for (int i = 1; i <= max; i++) {
if (seen[i]) continue;
ret += "(" + i;
seen[i] = true;
int buf = i;
while (!seen[gfi(f,g,i)]) {
i = gfi(f,g,i);
ret += ", " + i;
seen[i] = true;
}
i = buf;
ret += ")";
//Remove parentheses with only one element - i.e. (5)
if (ret.lastIndexOf(")") - ret.lastIndexOf("(") == 2) ret = ret.substring(0, ret.lastIndexOf("("));
}
if (ret.isEmpty() || !ret.contains(",")) return "Id";
return ret;
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> fStrings = new ArrayList<>();
ArrayList<String> gStrings = new ArrayList<>();
ArrayList<Permutation> fs = new ArrayList<>();
ArrayList<Permutation> gs = new ArrayList<>();
while (true) {
System.out.println("Enter a permutation on the column or press Enter to continue");
String s = in.readLine();
if (s.isEmpty()) break;
fStrings.add(s);
}
while (true) {
System.out.println("Enter a permutation on the row or press Enter to continue.");
System.out.println("Press Enter if the rows are the same as the columns");
String s = in.readLine();
if (s.isEmpty()) break;
gStrings.add(s);
}
//Find the maximum integer
int max = -1;
ArrayList<String> allStrings = new ArrayList<>();
allStrings.addAll(fStrings);
allStrings.addAll(gStrings);
for (String fString : allStrings) {
String[] a = fString.split("[^0-9]");
for (String s : a) {
if (s.isEmpty()) continue;
try {
int curr = Integer.parseInt(s);
if (curr > max) max = curr;
} catch (NumberFormatException e) { /* expected */}
}
}
for (String fString : fStrings) {
//Initialize all the permutations of fs
Permutation f = new Permutation(max);
if (fString.equals("Id")) {
fs.add(f);
continue;
}
String[] parenStrings = fString.split("\\)");
for (String parenString : parenStrings) {
parenString = parenString.replace("(", "");
parenString = parenString.replace(" ", "");
f.insertParen(parenString);
}
fs.add(f);
}
if (gStrings.isEmpty()) {
gStrings = (ArrayList<String>) fStrings.clone();
gs = (ArrayList<Permutation>) fs.clone();
}
else for (String gString : gStrings) {
//Initialize all the permutations of fs
Permutation g = new Permutation(max);
if (gString.equals("Id")) {
gs.add(g);
continue;
}
String[] parenStrings = gString.split("\\)");
for (String parenString : parenStrings) {
parenString = parenString.replace("(", "");
parenString = parenString.replace(" ", "");
g.insertParen(parenString);
}
gs.add(g);
}
for (int i = -1; i < fs.size(); i++) {
System.out.format("%30s", i == -1 ? "" : fStrings.get(i));
System.out.print(" ");
}
System.out.println();
System.out.print("------------------------------|");
for (int i = 0; i < fs.size(); i++) {
System.out.print("-------------------------------");
}
System.out.println();
for (int i = 0; i < gs.size(); i++) {
System.out.format("%30s", gStrings.get(i));
System.out.print("|");
for (int j = 0; j < fs.size(); j++) {
System.out.format("%30s", gAfterF(fs.get(j), gs.get(i)));
}
System.out.println();
}
// Permutation f = new Permutation(8);
// f.insertParen("6,8");
// f.insertParen("7,1");
// Permutation g = new Permutation(8);
// g.insertParen("6,8");
// g.insertParen("7,1");
}
}