题解 | #密码截取# 简便易行
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] c = in.nextLine().toCharArray(); int len = c.length; int maxLen = 0; // dp[i][j]表示i到j这一段是否为回文串 boolean[][] dp = new boolean[len][len]; // 初始化 for(int i=0;i<len;i++){ dp[i][i] = true; } for(int Len=2;Len<=len;Len++){ for(int i=0;i<len;i++){ int j = Len+i-1; if(j>len-1){break;} if(c[i] != c[j]){ dp[i][j] = false; }else{ if(Len<4){ dp[i][j] = true; }else{ dp[i][j] = dp[i+1][j-1]; } } if(dp[i][j] == true && Len > maxLen){ maxLen = Len; } } } System.out.println(maxLen); } }