找到最大回文

package palindrome;

import java.util.Stack;

/**
 * 找到最大回文
 * @author root
 */
public class FindMaxPalindrome {

	static String ss = "aaabcadasabcddcbaddbc";
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		find(ss.toCharArray());
	}
	public static void find(char []s){
		int max_l = 1;
		for(int i=0,j=0,k=0; k<s.length; k++){
			//char c = ss.charAt(k);
			int cur_l = 0;
			i=k+1;
			j=k-1;
			while(i<s.length && j>=0 && s[i]==s[j]){
				cur_l++;
				i++;
				j--;
			}
			if(cur_l>max_l){
				max_l = cur_l;
			}
			cur_l = 0;
			i=k+1;
			j=k;
			while(i<s.length && j>=0 && s[i]==s[j]){
				cur_l++;
				i++;
				j--;
			}
			if(cur_l>max_l){
				max_l = cur_l;
			}
		}
		System.out.println(max_l);
	}
}
package huiwen;

import java.util.Stack;
/**
 * 找到最大回文
 * @author Administrator
 *
 */
public class Huiwen {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "aabcbaded";
		hui(str.toCharArray());
	}
	static int begin = 0;
	static int length = 0;
	public static void hui(char []data){
		for(int i=1; i<data.length; i++){
			judge(data, i-1, i);
			judge(data, i-2, i);
		}
		print(data);
	}
	public static void judge(char []data,int left,int right){
		while(left>=0 && right<data.length && data[left]==data[right]){
			--left;
			++right;
		}
		int temp_length = right - left-1;
		if(length < temp_length){
			begin = left+1;
			length = temp_length;
			print(data);
		}
	}
	public static void print(char []data){
		System.out.println(begin+"*"+length);
		
		for(int i=0; i<length; i++){
			System.out.print(data[i+begin]);
		}
		System.out.println();
	}
}


posted on 2016-03-10 17:40  长456风  阅读(148)  评论(0编辑  收藏  举报

导航