大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public class MaxHw {public static String maxhw(String str){int n = str.length();
if ( n == 0 || n == 1) return str;
boolean[][] dp = new boolean[n][n];
//dp[i][j]为true表示字符索引i-j之间为回文子串
//若s.charAt(i) == s.charAt(j),那么只要dp[i+1][j-1]为true,dp[i][j]也必然为true
int start = 0;
int max = 1;
for (int i = 0; i< n; i++){dp[i][i] = true;
if (i< n - 1 && str.charAt(i) == str.charAt(i+1)){dp[i][i+1] = true;
start = i;
max = 2;
}
}
for (int m = 3; m<= n; m++){for (int i = 0; i + m - 1< n; i++){int j = m + i - 1;
if (str.charAt(i) == str.charAt(j) && dp[i+1][j-1] == true){dp[i][j] = true;
start = i;
max = m;
}
}
}
return str.substring(start,start + max);
}
public static void main(String[] args) {String a = "abba";
System.out.println(maxhw(a));
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧