实际上就是最长回文子串。采用动态规划解决即可。事件复杂度O(n^2).
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int maxlen = 0;
String str = in.nextLine();
char [] chs = str.toCharArray();
boolean dp[][] = new boolean[chs.length][chs.length];
for(int right = 1;right<chs.length;++right){
for(int left = right-1;left>=0;--left){
if(chs[left] == chs[right]){
if(right-left<=2)dp[left][right] = true;
else dp[left][right] = dp[left+1][right-1];
if(dp[left][right])maxlen = Math.max(maxlen,right-left+1);
}
}
}
System.out.println(maxlen);
}
}