题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String str = in.nextLine(); int res = 0; for (int i = 0; i < str.length(); i++) { StringBuilder sti = new StringBuilder(); StringBuilder stj = new StringBuilder(); int m = 0; for (int j = str.length() - 1; j >= 0; j--) { if ((i + m) >= str.length()) { if (pan(sti.toString(), stj.toString()) && m > res) res = m; break; } if (str.charAt(i + m) == str.charAt(j)) { sti.append(str.charAt(i + m)); stj.append(str.charAt(j)); m++; if (pan(sti.toString(), stj.toString()) && m > res) { res = m; } } else { if (pan(sti.toString(), stj.toString()) && m > res) res = m; sti.setLength(0); stj.setLength(0); j = j + m; m = 0; } } } System.out.println(res); } } public static boolean pan(String b, String c) { for (int i = 0; i < b.length(); i++) { if (b.charAt(i) != c.charAt(c.length() - 1 - i)) { return false; } } return true; } }