-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperpermAlgo.java
More file actions
92 lines (84 loc) · 2.45 KB
/
SuperpermAlgo.java
File metadata and controls
92 lines (84 loc) · 2.45 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
import java.io.*;
import java.util.Scanner;
public class SuperpermAlgo {
private final int n;
private int intersect;
private StringBuilder bead;
private PrintWriter writer;
public SuperpermAlgo(char[] c) {
n = c.length;
bead = new StringBuilder();
try {
writer = new PrintWriter("superperm_n" + n + ".txt");
} catch (IOException e) {
e.printStackTrace();
}
startBead(c);
algo(2);
writer.close();
}
public void startBead(char[] c) {
for (int i = 0; i < n; i++) {
bead.append(c[i]);
}
for (int i = 0; i < bead.length(); i++) {
writer.print(bead.charAt(i));
}
for (int i = 0; i < bead.length() - 1; i++) {
writer.print(bead.charAt(i));
}
}
public void algo(int d) {
if (d >= n) {
return;
}
for (int i = 0; i < d; i++) {
algo(d + 1);
intersect = d - 1;
if (i == d - 1) {
return;
}
mirrorShift(d);
}
}
public void mirrorShift(int index) {
int k = n - index;
String m = bead.substring(0, k);
for (int i = 0; i < n - k - 1; i++) {
bead.setCharAt(i, bead.charAt(i + k));
}
for (int i = n - k - 1; i < n - 1; i++) {
if (m.isEmpty()) {
break;
}
int l = m.length() - 1 - (i - (n - k - 1));
if (l < 0) {
continue;
}
bead.setCharAt(i, m.charAt(l));
}
for (int i = intersect; i < bead.length(); i++) {
writer.print(bead.charAt(i));
}
for (int i = 0; i < bead.length() - 1; i++) {
writer.print(bead.charAt(i));
}
}
public static char[] arrHelper(int n) {
char[] c = new char[n];
for (int i = 0; i < n; i++) {
c[i] = (char) ('0' + (i + 1));
}
return c;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of symbols in integer format:");
int n = input.nextInt();
char[] c = arrHelper(n);
long startTime = System.nanoTime();
new SuperpermAlgo(c);
long endTime = System.nanoTime();
System.out.println("\nExecution Time: " + (endTime - startTime) / 1e6 + " ms");
}
}