题解 | #密码截取#
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.; import java.io.;
public class Main{ public static void main(String[] args) { final InputStreamReader reader = new InputStreamReader(System.in); try( BufferedReader br = new BufferedReader(reader)){
String password = br.readLine();
char[] chars = password.toCharArray();
int size = 0;
for (int i = 1; i < chars.length-1; i++) {
size = Math.max(Math.max(getMaxSize(chars,i,i,true,false),getMaxSize(chars,i,i+1,true,true)),size);
}
System.out.println(size);
}catch (IOException e){
System.out.println(e.getMessage());
}
}
public static int getMaxSize(char[] chars,int i ,int j,boolean isFirts,boolean type){
//递归出口
if (i < 0 || j >chars.length-1) return 0;
if (type){
if (chars[i] == chars[j] && isFirts)
return 2+getMaxSize(chars,i-1,j+1,false,true);
}else {
if (chars[i] == chars[j] && isFirts)
return 1+getMaxSize(chars,i-1,i+1,false,false);
}
if (chars[i] == chars[j]) return 2+getMaxSize(chars,i-1,j+1,false,type);
return 0;
}
}