classSolution{ public String preProcess(String s){ int n = s.length(); if (n == 0) { return"^$"; } String ret = "^"; for (int i = 0; i < n; i++) ret += "#" + s.charAt(i); ret += "#$"; return ret; }
// 马拉车算法 public String longestPalindrome(String s){ String T = preProcess(s); int n = T.length(); int[] P = newint[n]; int C = 0, R = 0; for (int i = 1; i < n - 1; i++) { int i_mirror = 2 * C - i; if (R > i) { P[i] = Math.min(R - i, P[i_mirror]);// 防止超出 R } else { P[i] = 0;// 等于 R 的情况 }