-
Notifications
You must be signed in to change notification settings - Fork 368
/
Manachers_Algorithm.java
62 lines (49 loc) · 1.91 KB
/
Manachers_Algorithm.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
import java.util.Scanner;
public Manachers_Algorithm{
private static final int SIZE = 100000 + 1;
private static int[] P = new int[SIZE * 2];
// Transform S into new string Q with special characters inserted to avoid bound checking.
private static String convertToNewString(String s) {
StringBuilder newString = new StringBuilder("@");
for (int i = 0; i < s.length(); i++) {
newString.append("#").append(s.charAt(i));
}
newString.append("#$");
return newString.toString();
}
private static String longestPalindromeSubstring(String s) {
String Q = convertToNewString(s);
int c = 0, r = 0; // current center, right limit
for (int i = 1; i < Q.length() - 1; i++) {
// find the corresponding letter in the palindrome substring
int iMirror = c - (i - c);
if (r > i) {
P[i] = Math.min(r - i, P[iMirror]);
}
// expanding around center i
while (Q.charAt(i + 1 + P[i]) == Q.charAt(i - 1 - P[i])) {
P[i]++;
}
// Update c,r in case if the palindrome centered at i expands past r
if (i + P[i] > r) {
c = i; // next center = i
r = i + P[i];
}
}
// Find the longest palindrome length in p
int maxPalindrome = 0;
int centerIndex = 0;
for (int i = 1; i < Q.length() - 1; i++) {
if (P[i] > maxPalindrome) {
maxPalindrome = P[i];
centerIndex = i;
}
}
return s.substring((centerIndex - 1 - maxPalindrome) / 2, (centerIndex - 1 - maxPalindrome) / 2 + maxPalindrome);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
System.out.println(longestPalindromeSubstring(s));
}
}