由最长回纹子串引入动态规划编程思想
给定一个字符串求该字符串中的最长回纹子串public String longestPalindrome(String s) {
if(null==s || s.length()<2) return s;
int [] [] db=new int[s.length()][s.length()];
int start=0;
int maxLen=1;
for(int i=0;i<s.length();i++){
db[i][i]=1;
if(i<s.length()-1 && s.charAt(i)==s.charAt(i+1)){
db[i][i+1]=1;
start=i;
maxLen=2;
}
}
for(int span=3;span<=s.length();span++){
int i=0;
while(i<=s.length()-span){
int j=i+span-1;
if(s.charAt(i)==s.charAt(j)&& db[i+1][j-1]==1){
db[i][j]=1;
start=i;
maxLen=span;
}
i++;
}
}
return s.substring(start, start+maxLen);
}