package huiwen; import java.util.Stack; /** * 找到回文,由于无状态转移 * @author Administrator * */ public class RmHuiwen { public static void main(String[] args) { // TODO Auto-generated method stub String str = "edaabcbaded"; hui("",str.toCharArray(),0); System.out.println(longest); } static String longest = ""; public static void hui(String s,char []data, int index){ if(index>=data.length){ return; } //判断回文 if(judge(s) && longest.length()<s.length()){ longest = s; } hui(s, data, index+1); hui(s+data[index], data, index+1); } public static boolean judge(String s){ char[] data = s.toCharArray(); for(int i=0; i<data.length/2; i++){ if(data[i]!=data[data.length-1-i]){ return false; } } return true; } }