Java给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.

//给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 

//回文就是正反读都是一样的字符串,如aba, abba等 

import java.util.Scanner;

class  huiWen

{

public static void main(String[] args) 

{

Scanner sc=new Scanner(System.in);

String s1=sc.next();

//String s3=s1.substring(0,4);

//System.out.println(s3);

//char [] c12=s3.toCharArray();

//char [] c13=c12[c12.length];

//for(int i=0;i

//System.out.print("  "+c12[i]+"  ");

//}

//System.out.println(c12);

String s2=null;

int count=0,count2=0;

int [] sub=new int[(int)(Math.pow(2,s1.length()))];

for(int begin=0;begin<=s1.length()-1;begin++){

for(int end=begin+1;end<=s1.length();end++){

s2=s1.substring(begin,end);

System.out.println(s2);

char [] c2=s2.toCharArray();//截取的字符串转char数组c2

char [] c3=new char[c2.length];

//打印c2

for(int i=0;i

System.out.print("  "+c2[i]+"  ");

}

//c3是c2的翻转

for(int i=0;i

c3[c2.length-1-i]=c2[i];

}

System.out.println("-------");

//打印c3

for(int i=0;i

System.out.print("  "+c3[i]+" * ");

}//将得到的char数组转String

String c2s=String.copyValueOf(c2);

System.out.println(c2s+"=====");

String c3s=String.copyValueOf(c3);

System.out.println(c3s+"========");

//判断c2和c3是否相等

if(c2s.equals(c3s)){

//相等是回文

System.out.print("是回文");

System.out.println("---------------"+c2s.length());

count++;

sub[count-1]=c2s.length();

}else{

System.out.println("Nohui文");

count2++;

}

}

}

System.out.println("count="+count);

System.out.println("count总="+(count+count2));

for(int i=0;i<=count-1;i++){

System.out.println("hello------"+sub[i]);

}

int nummax=max(sub);

System.out.println("最大的回文串长度:"+nummax);

}

public static int max(int [] arr){

int num=arr[0];

for(int i=1;i

{

if(arr[i]>num){

num=arr[i];

}

}

return num;

}

}

你可能感兴趣的:(Java基础)