题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.Scanner; // 将每个字母作为中心来扩充 // 将奇偶两种情况写在一个循环里 // 注意初始化半径dis的条件 // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String line; while (in.hasNextLine()) { // 注意 while 处理多个 case line = in.nextLine(); if (line == null || line == "") break; int ans = 1; for (int i = 0; i < 2 * line.length() - 1; i++) { int dis = 0; int curLen = 1; int pre = (i - dis) / 2; int beh = (i + dis) / 2; while (pre >= 0 && beh < line.length() && line.charAt(pre) == line.charAt(beh)) { curLen = beh - pre + 1; if (dis == 0 && i % 2 == 0) { dis = 2; } if (dis == 0 && i % 2 != 0) { dis = 1; } pre = (i - dis) / 2; beh = (i + dis) / 2; dis += 2; } ans = Math.max(curLen, ans); } System.out.println(ans); } } }