题解 | #回文字符串#
回文字符串
http://www.nowcoder.com/practice/5bfb74efcd5449e69a480550b1fef431
就是这一小块,难了半天,最后看题解才发现递推公式搞错了。。。
we[i][j] = Math.max(we[i+1][j],we[i][j-1]);
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String s = input.next();
char[] list = s.toCharArray();
int j=0;
int li,rj;
int max =0;
int re=1;
int[][] we = new int[list.length][list.length];
for(int i =0;i<list.length;i++){
we[i][i] = 1;
}
for(int l =2;l<=list.length;l++){
for(int i = 0 ;i<list.length;i++){
max =0;
j = l+i -1;
if(j >=list.length){
break;
}
if(list[i] != list[j]){
//就是这一小块,难了半天,最后看题解才发现递推公式搞错了。。。
we[i][j] = Math.max(we[i+1][j],we[i][j-1]);
}else{
if(j-i<2){
we[i][j] = 2;
}
else{
we[i][j] = we[i+1][j-1] + 2;
}
}
if(we[i][j] > re){
re = we[i][j];
}
}
}
System.out.print(re);
}
}