题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.Scanner; // 服咯,竟然做了40分钟,中等题做的好慢啊 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String p = in.nextLine(); char[] chars = p.toCharArray(); int max = 0; for(int i=0;i<chars.length - 2;i++) { if (chars[i] == chars[i+1]) { int len = i + 1 > chars.length - i - 1 ? chars.length - i - 1 : i + 1; int temp = 0; for(int j=0;j < len; j++) { if(chars[i - j] == chars[i+1+j]) { temp++; } else { break; } } max = max < 2 * temp ? 2 * temp : max; } if (chars[i] == chars[i+2]) { int len = i + 1 > chars.length - i - 2 ? chars.length - i - 2 : i + 1; int temp = 0; for(int j=0;j < len; j++) { if(chars[i - j] == chars[i+2+j]) { temp++; } else { break; } } max = max < 2 * temp + 1 ? 2 * temp + 1 : max; } } System.out.println(max); } }