中心扩散法| #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.Scanner; import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); scanner.useDelimiter("\n"); while (scanner.hasNext()){ String S = scanner.next(); int max = 0; for (int i = 0; i < S.length(); i++) { int ABA = add(S, i, i); int ABBA = add(S, i, i+1); max = Math.max(ABA, Math.max(ABBA, max)); } System.out.println(max); } } public static int add(String S, int left, int right) { while (left>=0&&right<=S.length()-1&&S.charAt(left)==S.charAt(right)) { left--; right++; } return right-left-1; } }