-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManachers.java
63 lines (49 loc) · 1.73 KB
/
Manachers.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
54
55
56
57
58
59
60
61
62
63
package algorithms.floatfoo;
public class Manachers {
public static String lps(String s) {
// trivial cases
if (s == null || s.length() == 0) return "";
// create hashed version of array
char[] hashed= new char[2*s.length() + 1];
prework(hashed, s);
int max = 0;
int C = 0, R = 0;
int[] lps = new int[hashed.length];
for (int i = 1; i < hashed.length; i++) {
// first step - if mirror update
//
// second - extend
//
// check for right bound violation
if ( i < R && i >= C ) {
lps[i] = Math.min(lps[C - ( i - C )], R - i);
if (i == 7) {
}
}
while (i - lps[i] - 1 >= 0 && i + lps[i] + 1 < hashed.length && hashed[i - lps[i] - 1] == hashed[i + lps[i] + 1]) {
lps[i]++;
}
if ( i + lps[i] > R ) {
max = lps[max] >= lps[i] ? max : i;
C = i;
R = i + lps[i];
}
}
StringBuilder res = new StringBuilder();
for (int i = max - lps[max] ; i <= max + lps[max]; i++) {
if (hashed[i] != '#') {
res.append(hashed[i]);
}
}
return res.toString();//s.substring(max/2 - lps[max]/2, max/2 + lps[max]/2) - this should also work, but some cases do not work as intendent
}
private static void prework(char[] hash, String s) {
for (int i = 0; i < hash.length; i++) {
hash[i] = i % 2 == 0 ? '#' : s.charAt(i / 2) ;
}
}
public static void main(String[] args) {
String test = lps("cbbd");
System.out.println(test);
}
}