题解 | #密码截取#
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; import java.util.List;
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextLine()) { String s = sc.nextLine(); char[] test = s.toCharArray(); // Create a new array to store all the symmetric password length ArrayList as = new ArrayList(); a:for(int i = 0; i < test.length; i++) { b:for(int j = test.length - 1; j >= i; j--) { if(test[i] == test[j]) { //Consider there is only three elements in String if( j - i == 2) { as.add(3); // If the array length is 1 } else if(j == i && test.length == 1) { as.add(1); } else if(j - i > 2) { int ck = (j-i)/2, cka = 0; for(int k = 1; k <= (j-i)/2; k++) { if(test[i+k] == test[j-k]) { cka++; } } if(ck == cka) { as.add(j - i + 1); } } } } } int max = 0; //Compare the length, find the max for(int l = 0; l < as.size(); l++) { if(max < as.get(l)) { max = as.get(l); } } System.out.println(max); } } }