Created
February 5, 2017 03:43
-
-
Save Chen-tao/5c93558a18dd1d5e5efc91b3a5c9a581 to your computer and use it in GitHub Desktop.
Manacher算法,时间复杂度O(n), 空间复杂度O(n)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public String longestPalindrome(String s) { | |
String T = preProcess(s); | |
int n = T.length(); | |
int[] p = new int[n]; | |
int center = 0, right = 0; | |
for (int i = 1; i < n - 1; i++) { | |
int j = 2 * center - i; //j and i are symmetric around center | |
p[i] = (right > i) ? Math.min(right - i, p[j]) : 0; | |
// Expand palindrome centered at i | |
while (T.charAt(i + 1 + p[i]) == T.charAt(i - 1 - p[i])) | |
p[i]++; | |
// If palindrome centered at i expand past right, | |
// then adjust center based on expand palindrome | |
if (i + p[i] > right) { | |
center = i; | |
right = i + p[i]; | |
} | |
} | |
// Find the longest palindrome | |
int maxLength = 0, centerIndex = 0; | |
for (int i = 1; i < n - 1; i++) { | |
if (p[i] > maxLength) { | |
maxLength = p[i]; | |
centerIndex = i; | |
} | |
} | |
centerIndex = (centerIndex - 1 - maxLength) / 2; | |
return s.substring(centerIndex, centerIndex + maxLength); | |
} | |
// preProcess the original string s. | |
// For example, s = "abcdefg", then the rvalue = "^#a#b#c#d#e#f#g#$" | |
private String preProcess(String s) { | |
if (s == null || s.length() == 0) return "^$"; | |
StringBuilder rvalue = new StringBuilder("^"); | |
for (int i = 0; i < s.length(); i++) | |
rvalue.append("#").append(s.substring(i, i+1)); | |
rvalue.append("#$"); | |
return rvalue.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment