-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLyndonDecomposition.java
53 lines (49 loc) · 1.03 KB
/
LyndonDecomposition.java
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
package Strings;
import java.util.*;
public class LyndonDecomposition {
public static String minCyclicShift(String a) {
char[] s = (a + a).toCharArray();
int n = s.length;
int res = 0;
for (int i = 0; i < n / 2; ) {
res = i;
int j = i + 1, k = i;
while (j < n && s[k] <= s[j]) {
if (s[k] < s[j])
k = i;
else
++k;
++j;
}
while (i <= k) i += j - k;
}
return new String(s, res, n / 2);
}
public static String[] decompose(String s) {
List<String> res = new ArrayList<>();
char[] a = s.toCharArray();
int n = a.length;
int i = 0;
while (i < n) {
int j = i + 1, k = i;
while (j < n && a[k] <= a[j]) {
if (a[k] < a[j])
k = i;
else
++k;
++j;
}
while (i <= k) {
res.add(s.substring(i, i + j - k));
i += j - k;
}
}
return res.toArray(new String[0]);
}
public static void main(String[] args) {
String s = "bara";
String[] decompose = decompose(s);
System.out.println(Arrays.toString(decompose));
System.out.println(minCyclicShift(s));
}
}