目的:在任意的字符串中求出最长的回文字符串
思路:(适用于任何语言)
1、判断当前给定的字符串是否是相同的字符串(也就是所有字符都相同),如果是直接返回了。
2、如果第一步没有返回,就以非第一个字符为轴,分别求出以它为轴的,双数回文字符串,和单数回文字符串的长度。
3、把上述求出来的长度,和已经有的回文字符串长度对比,如果长于已经存在的回文字符串的长度,就进行赋值。
4、判断最后统计的回文字符串如果等于空,并且给出的字符大于0,就把第一个字符赋值给最长回文字符串,并且返回。
代码如下:
public class Test {
public String aa(String s){
char[] arr = s.toCharArray(); //把字符串转换成字符
String max = ""; //收集最长的回文字符串
int k = 0; //统计当前 最长回文字符串的长度 / 同时统计以当前字符串为轴的时候 双数回文字符串的长度
int flag = 0; //0 表示 以该字符为轴的是 双数回文字符串
int m = 0; //
int p = 0; //统计以当前字符串为轴的时候 单数回文字符串的长度
//如果字符串全部是一种字符的话 直接返回
for(k = 0;k < arr.length-1; k++){
if (arr[k] != arr[k+1])
break;
}
if(k == arr.length -1)
return s;
//以非第一个字符为轴 ,来统计以它为轴 双数回文字符串 和 单数 回文字符串的长度 并且把最长的回文字符串 赋值给 max
for(int i = 1;i < arr.length; i++){
k = 0;
p = 0;
for (flag = 0;flag <= 1; flag++)
for(int j = i-1;j >= 0; j--){
if ( flag == 1 && j == i-1 )
m = 0;
if(flag == 0){ //双数
if(i+k < arr.length)
if(arr[j] == arr[i+k]){
k++;
}else {
break;
}
}
else { //单数
if(j >= 0 && i+m+1 < arr.length )
if(arr[j] == arr[i+1+(m++)]){
p++;
}else {
break;
}
}
}
if ( p >= k ){ // k 表示双 p表示单
k = p;
flag = 1;
} else
flag = 0;
if(flag == 0){
if(max.length() < 2*k){
max = "";
for(int j = i-k;j < i+k; j++){
max += arr[j];
}
}
} else{
if(max.length() < 2*k+1){
if(i - k >= 0 && i+k <= s.length()-1){
max = "";
for(int j = i-k;j <= i+k; j++){
max += arr[j];
}
}
}
}
}
if (max.equals("") && arr.length > 0) //当输入空串和没有任何长度大于2的回转字符做的特殊处理
max += arr[0];
return max;
}
public static void main(String[] args) {
Test t = new Test();
//测试数据
System.out.println(t.aa("lqlvciwekzxapmjxyddlaoqhfhwphamsyfwjinkfvciucjhdgwodvmnpkibumexvlsxxumvrznuuyqfavmgwfnsvfbrvqhlvhpxaqehsiwxzlvvtxsnmlilbnmvnxyxitxwoahjricdjdncvartepfpdfndxqoozkfpdmlpvshzzffsspdjzlhmamqooooor"));
System.out.println(t.aa("oooooor"));
System.out.println(t.aa("aba"));
System.out.println(t.aa("abb"));
System.out.println(t.aa("bba"));
System.out.println(t.aa("aaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnooooooooooppppppppppqqqqqqqqqqrrrrrrrrrrssssssssssttttttttttuuuuuuuuuuvvvvvvvvvvwwwwwwwwwwxxxxxxxxxxyyyyyyyyyyzzzzzzzzzzyyyyyyyyyyxxxxxxxxxxwwwwwwwwwwvvvvvvvvvvuuuuuuuuuuttttttttttssssssssssrrrrrrrrrrqqqqqqqqqqppppppppppoooooooooonnnnnnnnnnmmmmmmmmmmllllllllllkkkkkkkkkkjjjjjjjjjjiiiiiiiiiihhhhhhhhhhggggggggggffffffffffeeeeeeeeeeddddddddddccccccccccbbbbbbbbbbaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnooooooooooppppppppppqqqqqqqqqqrrrrrrrrrrssssssssssttttttttttuuuuuuuuuuvvvvvvvvvvwwwwwwwwwwxxxxxxxxxxyyyyyyyyyyzzzzzzzzzzyyyyyyyyyyxxxxxxxxxxwwwwwwwwwwvvvvvvvvvvuuuuuuuuuuttttttttttssssssssssrrrrrrrrrrqqqqqqqqqqppppppppppoooooooooonnnnnnnnnnmmmmmmmmmmllllllllllkkkkkkkkkkjjjjjjjjjjiiiiiiiiiihhhhhhhhhhggggggggggffffffffffeeeeeeeeeeddddddddddccccccccccbbbbbbbbbbaaaa"));
System.out.println(t.aa("cbbd"));
System.out.println(t.aa("fttttf"));
System.out.println(t.aa("eabcb"));
System.out.println(t.aa("aaabaaa"));
System.out.println(t.aa("aaabaaaaaa"));
System.out.println(t.aa("ac"));
System.out.println(t.aa(""));
System.out.println(t.aa("sooos"));
System.out.println(t.aa("azwdzwmwcqzgcobeeiphemqbjtxzwkhiqpbrprocbppbxrnsxnwgikiaqutwpftbiinlnpyqstkiqzbggcsdzzjbrkfmhgtnbujzszxsycmvipjtktpebaafycngqasbbhxaeawwmkjcziybxowkaibqnndcjbsoehtamhspnidjylyisiaewmypfyiqtwlmejkpzlieolfdjnxntonnzfgcqlcfpoxcwqctalwrgwhvqvtrpwemxhirpgizjffqgntsmvzldpjfijdncexbwtxnmbnoykxshkqbounzrewkpqjxocvaufnhunsmsazgibxedtopnccriwcfzeomsrrangufkjfzipkmwfbmkarnyyrgdsooosgqlkzvorrrsaveuoxjeajvbdpgxlcrtqomliphnlehgrzgwujogxteyulphhuhwyoyvcxqatfkboahfqhjgujcaapoyqtsdqfwnijlkknuralezqmcryvkankszmzpgqutojoyzsnyfwsyeqqzrlhzbc"));
}
}