题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); int n = str.length(); boolean[][] dp = new boolean[n][n]; int max = 0; for(int i = 0; i < n; i++){ for(int j = 0; j <= i; j++){ if(str.charAt(j) == str.charAt(i) && (i - j < 2 || dp[j + 1][i - 1] == true)){ dp[j][i] = true; max = Math.max(max,i - j + 1); } } } System.out.println(max); } }