题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.Scanner; //建议参考HJ85最长回文字符串,一模一样 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] str = in.nextLine().toCharArray(); int max = 0; int[][] dp = new int[str.length + 1][str.length + 1]; for (int i = 1; i <= str.length; i++) { dp[i][i] = 1; } for (int i = str.length - 1; i >= 1; i--) { for (int j = i + 1; j <= str.length; j++) { if (j - i == 1) { if (str[i - 1] == str[j - 1]) { dp[i][j] = 1; max = Math.max(j - i + 1, max); } else { dp[i][j] = 0; } } else { if (str[i - 1] == str[j - 1] && dp[i + 1][j - 1] != 0) { dp[i][j] = 1; max = Math.max(j - i + 1, max); } else { dp[i][j] = 0; } } } } System.out.println(max); } }
华为OD机试 文章被收录于专栏
自己在准备机试,记录一下学习轨迹,主要参考真题,代码大部分是自己想的,不保证ac,仅供参考